ActionScript 3 FlxG.keys error - actionscript-3

So I have more errors like these:
Player.as(21): col: 25 Error: Access of undefined property FlxG.
on this code:
override public function update():void
{
super.update();
velocity.x = 0;
var right:Boolean = (FlxG.keys.RIGHT || FlxG.keys.D);
var left:Boolean = (FlxG.keys.LEFT || FlxG.keys.A);
var up:Boolean = (FlxG.keys.UP || FlxG.keys.W);
how can I fix it?
also I am begginer to actionscript3 and I don't know too many terms. Thanks for helping!

FlxG it is a Flixel Class. To fix this issue you should download Flixel library and place org folder near your FLA file.

Related

ActionScript3:Undefined property error

I'm making a toggle button to resume/pause the audio in Adobe flash CS 5:
I used a Code Snippet "click to play/stop Sound".
Here is the code:
pause_play_button.addEventListener(MouseEvent.CLICK,fl_ClickToPlayStopSound_2);
var fl_ToPlay_2:Boolean = true;
var resumeTime:Number = 0.00;
var s:Sound = new Tanishma_Sound();
var fl_SC_2:SoundChannel ;
function fl_ClickToPlayStopSound_2(evt:MouseEvent):void
{
if(fl_ToPlay_2)
{
f1_SC_2 = s.play (resumeTime);
}
else
{
resumeTime = f1_SC_2.position;
f1_SC_2.stop ();
}
fl_ToPlay_2 = !fl_ToPlay_2;
}
I have this error and I don't know how to fix it:
Scene 1, Layer 'Actions', Frame 1, Line 47 1120: Access of undefined property f1_SC_2.
Any Help!
That error means that Flash can't find something you've referenced. In your case, this is because of a syntax typo.
You have defined: (note the f then the letter l)
var fl_SC_2:SoundChannel;
Yet later on, you've change the 'l' to the numeral '1' in three places.
f1_SC_2
Should be:
if(fl_ToPlay_2)
{
fl_SC_2 = s.play (resumeTime);
}
else
{
resumeTime = fl_SC_2.position;
fl_SC_2.stop ();
}

cannot convert flash.display::Stage#2a2cdf99 to flash.display.MovieClip?

So I'm creating a platform game in Actionscript 3.0, trying to call a function that spawns blocks based on an array. The code is in a 'Game' class and is directed towards a movieclip on my .fla
When it is ran I get the error:
"cannot convert flash.display::Stage#2a2cdf99 to flash.display.MovieClip."
Here's the code:
public function GameScreen(stageRef:Stage = null )
{
this.stageRef = stageRef;
btnReturn.addEventListener(MouseEvent.MOUSE_DOWN, returnMainMenu, false, 0, true);
mcMain.addEventListener(Event.ENTER_FRAME, moveChar);
this.addEventListener(Event.ENTER_FRAME, createLvl);
this.stageRef.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
this.stageRef.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
this.stageRef.addChild(blockHolder);
}
And
private function createLvl(event:Event):void
{
var lvlArray:Array = MovieClip(root)['lvlArray' +lvlCurrent];
var lvlColumns:int = Math.ceil(lvlArray.length/16);
for(var i:int = 0;i<lvlArray.length;i++){
if(lvlArray[i] == 1){
if(i/lvlColumns == int(i/lvlColumns)){
row ++;
}
var newBlock:Block = new Block();
newBlock.graphics.beginFill(0xFFFFFF);
newBlock.graphics.drawRect(0,0,50,50);
newBlock.x = (i-(row-1)*lvlColumns)*newBlock.width;
newBlock.y = (row - 1)*newBlock.height;
blockHolder.addChild(newBlock);
} else if (lvlArray[i] == 'MAIN'){
mcMain.x = (i-(row-1)*lvlColumns)*newBlock.width;
mcMain.y = (row-1)*newBlock.height;
}
}
row = 0;
}
Please help =(
Thanks!
First of all:
Turn on "permit debugging" in your publish settings. This will give you line numbers with your errors, so you can determine the exact location of your error.
Post the entire stack trace. That error by itself is not a lot to go on.
Given the error and the code you've posted, the error must be caused by your use of MovieClip(root). The root property does not always point to the main timeline in Flash, it will point to the Stage if the display object is added directly to the stage. For example:
trace(stage.addChild(new Sprite()).root) // [object Stage]
Documentation on root: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html#root

as3 1119: Access of possibly undefined property getters/setters

It would be amazing if someone could expand on the current answer, Thanks.
full error
Line 22 1119: Access of possibly undefined property CharacterX through a reference with static type flash.display:DisplayObject.
I'm trying to set a variable for the object shark, that is already defined in the object character
First time using setters in flash, so I might not be doing this right.
code I'm using to set the variable I tried to comment out the stuff I thought was irrelevant to this issue, not actually commented out in real code.
var character:Character;
//var bullet:Bullet=null;
//var bullets:Array = new Array();
//var enemies:Array = new Array();
//character=new Character(bullets);
addChild(character);
var shark:Shark=new Shark();
addChild(shark);
//var enemy:Enemy=null;
////var i:int;
//for (i=0; i<10; i++) {
//enemy = new Enemy(Math.random()*stage.stageWidth, Math.random()*stage.stageHeight);
//addChild(enemy);
// enemies.push(enemy);
//}
//stage.addEventListener(Event.ENTER_FRAME, colTest);
//function colTest(e:Event ):void {
// if(character.hitTestObject(turtle)){
// character.gotoAndStop("Turtle");
// }
//}
shark.setT(character.x, character.y)
class in which I'm attempting to define a variable using the function above.
package
{
import flash.display.*;
import flash.events.*;
public class Shark extends MovieClip
{
var CharacterX:Number = 0;
var CharacterY:Number = 0;
public function Shark()
{
this.x = 300;
this.y = 200;
addEventListener(Event.ENTER_FRAME,playGame);
}
public function setT(characterx:Number,charactery:Number){
CharacterX = characterx - this.x;
CharacterY = charactery - this.y;
}
function playGame(event:Event):void
{
var ease:int = 20;
var speed:int = 10;
var targetX:int = root.CharacterX - this.x;
var targetY:int = root.CharacterY - this.y;
var rotation = Math.atan2(targetY,targetX) * 180 / Math.PI;
cut code off here, didn't want to make a code dump can get you anything that might be relevant just ask.
Here is a pastebin of all of the code if it might help,
Shark class:
Actions on Frame 1:
Character class
Let me start by saying I can't spot the exact problem here, but I have some ideas. Your error 1999 says that something of the type display object is trying to change your variable. This happens a lot when you use parent.myMethod() because parent is typed as display object. You would fix this by typecasting like (parent as MovieClip).myMethod
In your case I don't see the exact source of this problem. But you could try using this.characterX in your setT function

AS3 Cannot access a property or method of null object reference, issue with gotoAndPlay

so I'm still fairly new to AS3 and I'm getting my head around the errors and stuff and I'm trying to figure out why this is not working how it should, the function still executes but it leaves an error on my console. Here is my code:
import flash.events.MouseEvent;
import flash.display.MovieClip;
stop();
var activeHitArray:Array = new Array(word_select_box);
var activeDropArray:Array = new Array(answer1_word, answer2_word, answer3_word);
var hitPositionsArray: Array = new Array();
for (var numi:int = 0; numi < activeDropArray.length; numi++) {
activeDropArray[numi].buttonMode = true;
activeDropArray[numi].addEventListener(MouseEvent.MOUSE_DOWN, mousedown1);
activeDropArray[numi].addEventListener(MouseEvent.MOUSE_UP, mouseup1);
hitPositionsArray.push({xPos:activeDropArray[numi].x, yPos:activeDropArray[numi].y});
}
function mousedown1(event:MouseEvent):void {
event.currentTarget.startDrag();
setChildIndex(MovieClip(event.currentTarget), numChildren - 1);
}
function mouseup1(event:MouseEvent):void {
var dropindex1:int = activeDropArray.indexOf(event.currentTarget);
var target:MovieClip = event.currentTarget as MovieClip;
target.stopDrag();
if(target.hitTestObject(activeDropArray[dropindex1])){
// target.x = activeHitArray[dropindex1].x;
//target.y = activeHitArray[dropindex1].y;
if(answer1_word.hitTestObject(word_select_box)){
gotoAndStop("6");
}
} else {
target.x = hitPositionsArray[dropindex1].xPos;
target.y = hitPositionsArray[dropindex1].yPos;
}
}
And the Error I'm getting through is:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at game_flv_fla::MainTimeline/frame6()
at flash.display::MovieClip/gotoAndStop()
at game_flv_fla::MainTimeline/mouseup1()
The only thing I can think of is that it is something to do with the gotoAndPlay() because when I place a trace into it's place I get no errors.
I copied your code, compiled and lauched it in Flash IDE. It works! :)
There were no errors.
But I know where the isue may lay. The addEventListeners are still on, no matter if there still are the objects they were linked to. You need to clean all active things before going to next frame:
if(target.hitTestObject(activeDropArray[dropindex1])){
if(answer1_word.hitTestObject(word_select_box)){
for (var i:uint = 0; i < activeDropArray.length; i++) {
activeDropArray[i].removeEventListener(MouseEvent.MOUSE_DOWN, mousedown1);
activeDropArray[i].removeEventListener(MouseEvent.MOUSE_UP, mouseup1);
}
gotoAndStop("6");
}
}

Error 1009 cannot access a property----again

Hello I'm stuck on this one which is trying to show a game level on the game and getting an output error 1009. I'm doing something wrong but can't figure. Here is the code:
if ( levelNumber ==1) {
var level:Number = 1;
showLevel.text=level.toString();
showLevel.text = String ("Level: " +level);
if ( levelNumber ==2) {
var level:Number = 2;
showLevel.text=level.toString();
showLevel.text = String("Level: " +level);
var numBombs:Number = 4;
}else if( levelNumber ==3 ) {
var level:Number = 3;
showLevel.text=level.toString();
showLevel.text = String ("Level: " +level);
The debugger says it's the line with showLevel.text=level.toString(); which I have set as a Number ie private var level:Number =0. I hope this is sufficient code to let you know what's going on here. It's also coming up with a duplicate variable definition - compiler error on var level:Number =2 and the next one that =3.Thanking you in advance for any assistance. Cheers.
Error #1009 is indeed a null reference error. I see your level is declared and has a right value.
Maybe the point is that your showLevel object (I guess it's a label) is not declared yet.
The problem can be that you use:
private var showLevel:mx.controls.Label;
instead of:
private var showLevel:mx.controls.Label = new mx.controls.Label();
otherwise if you make your showLevel in a viewStack you must be sure that your content is created yet. Therefor you can use:
<mx:ViewStack id="yourView" creationPolicy="all">