Im trying to develop simple pawn (player) and enemy classes but there is 2 story and each have their problems.
first: when the pawn and enemies are added to stage by code in the document class I have the advantage to use them by instance name I used to add them for things like hit test (I guess)
second: when I add them to the stage by dragging them from library to stage. this way I can add event listeners to the stage inside them (for movements) but accessing them gets hard and sometimes the null stage is my biggest problem.
any suggestion to make things easy?
for example In the pawn class below I added keyboard listener to the stage but when I add this pawn to the stage by add child I get access to null ... error
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class pwn extends MovieClip {
public var upPressed:Boolean = false;
public var downPressed:Boolean = false;
public var leftPressed:Boolean = false;
public var rightPressed:Boolean = false;
public var lastMove:String;
public static var spd:Number=6;
public function pwn() {
addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);
}
//Movement
public function fl_MoveInDirectionOfKey(event:Event){
if (leftPressed){
x -= spd;
if(currentFrame < 18 || currentFrame > 24){
gotoAndPlay(17);
}
}else
if (rightPressed){
x += spd;
if(currentFrame < 26 || currentFrame > 31){
gotoAndPlay(25);
}
}else
if (upPressed){
y -= spd;
if(currentFrame < 10 || currentFrame > 16){
gotoAndPlay(9);
}
}else
if (downPressed){
y += spd;
if(currentFrame < 2 || currentFrame > 8){
gotoAndPlay(1);
}
}
}
public function fl_SetKeyPressed(event:KeyboardEvent):void{
switch (event.keyCode){
case 87:
case 38:{
upPressed = true;
break;
}
case 83:
case 40:{
downPressed = true;
break;
}
case 65:
case 37:{
leftPressed = true;
break;
}
case 68:
case 39:{
rightPressed = true;
break;
}
}
}
public function fl_UnsetKeyPressed(event:KeyboardEvent):void{
switch (event.keyCode){
case 87:
case 38:{
upPressed = false;
break;
}
case 83:
case 40:{
downPressed = false;
break;
}
case 65:
case 37:{
leftPressed = false;
break;
}
case 68:
case 39:{
rightPressed = false;
break;
}
}
}
}
}
what do you in these situations?