Question

So I'm trying to make a spaceship fire lasers when the spacebar is pressed. I've done this before in a pure flex project but have recently gotten creative cloud and am trying to recreate the same effect using flash professional/flash builder.

Unfortunately when I create a new instance of my "Laser" class and try and put it on the stage with addChild() nothing seems to happen.

Here is the main file/document class

    public class PlayerShip extends Sprite
    {

        private var laserTimer:Timer;
        private var shipTime:Timer;
        private var upKey:Boolean;
        private var downKey:Boolean;
        private var leftKey:Boolean;
        private var rightKey:Boolean;
        private var spacebar:Boolean;
        private var utils:Utils = new Utils();

        //tuning variables
        private var MOVE_SPEED:int = 5;
        private var REVERSE_SPEED:int = 3;
        private var TURN_SPEED:int = 5;

        private var laserEmitter:shipLasers = new shipLasers(stage);

        public function PlayerShip():void
        {
            super();

            addEventListener(Event.ENTER_FRAME, fly);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, movementKeysDown);
            stage.addEventListener(KeyboardEvent.KEY_UP, movementKeysUp);

            laserTimer = new Timer(1000/1000);
            laserTimer.addEventListener(TimerEvent.TIMER, fireLasers);
            laserTimer.start();
            addChild(laserEmitter);
        }

        public function fly(e:Event):void {
            if(downKey) {
                SpaceShip.x -= Math.sin(utils.degreesToRadians(SpaceShip.rotation)) * REVERSE_SPEED;
                SpaceShip.y += Math.cos(utils.degreesToRadians(SpaceShip.rotation)) * REVERSE_SPEED;
            }
            if(upKey) {
                SpaceShip.x += Math.sin(utils.degreesToRadians(SpaceShip.rotation)) * MOVE_SPEED;
                SpaceShip.y -= Math.cos(utils.degreesToRadians(SpaceShip.rotation)) * MOVE_SPEED;
            }
            if(leftKey) {
                SpaceShip.rotation -= TURN_SPEED;
            }
            if(rightKey) {
                SpaceShip.rotation += TURN_SPEED;
            }
        }

        public function movementKeysUp(e:KeyboardEvent):void { //rotators is key_up :P
            switch(e.keyCode) {
                case 83:
                    downKey = false;
                    break;
                case 65:
                    leftKey = false; // on "a" key_up sets left turn to false. Simple enough.
                    break;
                case 68:
                    rightKey = false; // K. "d" released makes me not turn right.
                    break;
                case 87:
                    upKey = false; // I guess case 87 is "w"
                    break;
                case 32:
                    spacebar = false;
                    break;
            }
        }

        public function movementKeysDown(e:KeyboardEvent):void { // key_down for movers
            switch(e.keyCode) {
                case 83:
                    downKey = true;
                    break;
                case 65:
                    leftKey = true; //so now on key_down for the "a" key it makes me go left! :D
                    break;
                case 68:
                    rightKey = true; //same as lft...
                    break;
                case 87:
                    upKey = true;
                    break;
                case 32:
                    spacebar = true;
                    break;
            }
        }

        public function fireLasers(e:TimerEvent) {
            if(spacebar) {
                laserEmitter.Emit(SpaceShip.x, SpaceShip.y, SpaceShip.rotation);
                addChild(laserEmitter);
            }
        }

        public function getShip():MovieClip {
            return SpaceShip;
        }
    }
}

and this is the separate class that is supposed to create new instances of the Laser class and put them on the stage.

    public class shipLasers extends Sprite implements Emittable
    {
        var tempLaserRight:MovieClip;
        var tempLaserLeft:MovieClip;
        var laserArray:Array = [];

        public function shipLasers(stage:Stage):void
        {
        }


        public function Emit(x:int, y:int, rotation:Number):void {
                tempLaserRight = new Laser();
                tempLaserLeft = new Laser();

                tempLaserRight.rotation = tempLaserLeft.rotation = rotation;
                tempLaserRight.x = 200;
                tempLaserLeft.x = 210;
                tempLaserRight.y = 200;
                tempLaserLeft.y = 200;

                laserArray.push(tempLaserRight);
                laserArray.push(tempLaserLeft);
                stage.addChild(tempLaserRight);
                stage.addChild(tempLaserLeft);

                trace("Oh come on!");
        }
    }
}

Thanks!

Was it helpful?

Solution

You never store passed stage reference in shipLasers class, thus you are trying to refer its own built-in stage ref, which is likely null because your instances of shipLasers don't get added to display list themselves. You need to store the stage ref passed in the constructor and use that to add children.

 public class shipLasers ... {
     var theStage:Stage;
     public function shipLasers(aStage:Stage){
         theStage = aStage;
     }
     public function Emit(...) { 
         ...
         theStage.addChild(tempLaserRight);
         thestage.addChild(tempLaserLeft);
     }
 }

Update: It's also a good practice to first check stage availability, then use stage reference. To do this, you need to listen to Event.ADDED_TO_STAGE event in your ship class, as it uses stage left right and center. The common code of achieving this is as follows:

public function PlayerShip() {
    ....
    // leave here only code that does not require stage 
    if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE,init);
}
private function init(e:Event=null):void {
    removeEventListener(Event.ADDED_TO_STAGE,init);
    // here place all code that's left from your initialization process
    laserEmitter=new shipLasers(stage);
    stage.addEventListener(KeyboardEvent.KEY_DOWN, movementKeysDown);
    stage.addEventListener(KeyboardEvent.KEY_UP, movementKeysUp);
    // etc.
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top