Search code examples
arraysactionscript-3flashflash-cc

Trouble creating multiple objects (Arrays) in Flash


I'm making a basic Galaga type game in Flash and have been running into issues. This is my first time really messing with ActionScript.
I created an Array for my Enemies and would like to know how I would go about having them spawn in their own respective locations, like in Galaga and then have them move uniformly form left to right while descending once reaching the edge of the stage.

Game

package
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;
    public class SpaceVigilanteGame extends MovieClip

    {
        public var army:Array;
        public var avatar:Avatar;
        public var gameTimer:Timer;
        public var useMouseControl:Boolean; 
        public var rightKeyIsBeingPressed:Boolean;
        public var leftKeyIsBeingPressed:Boolean; 
        var gameWidth:int = 0;
        var gameHeight:int = 0;

        public function SpaceVigilanteGame()
        {   useMouseControl = false;
            leftKeyIsBeingPressed = false;
            rightKeyIsBeingPressed = false;
            army = new Array();
            var newEnemy = new Enemy( 60, 30 );
            army.push( newEnemy );
            addChild( newEnemy );
            avatar = new Avatar();
            addChild( avatar );

            if ( useMouseControl )
            {
                avatar.x = mouseX;
                avatar.y = mouseY;
            }
            else
            {
                avatar.x = 50;
                avatar.y = 400;
            }

            gameWidth = stage.stageWidth;
            gameHeight = stage.stageHeight;


            gameTimer = new Timer( 25 );
            gameTimer.addEventListener( TimerEvent.TIMER, moveEnemy );
            gameTimer.addEventListener( TimerEvent.TIMER, moveAvatar );
            gameTimer.start();
            stage.addEventListener( KeyboardEvent.KEY_DOWN, onKeyPress );
            stage.addEventListener( KeyboardEvent.KEY_UP, onKeyRelease );

        function onKeyPress( keyboardEvent:KeyboardEvent ):void
        {
            if ( keyboardEvent.keyCode == Keyboard.RIGHT )
            {
                rightKeyIsBeingPressed = true;
            }
            else if ( keyboardEvent.keyCode == Keyboard.LEFT ) 
            {
                leftKeyIsBeingPressed = true;
            }
        }
        function onKeyRelease( keyboardEvent:KeyboardEvent ):void
        {
            if ( keyboardEvent.keyCode == Keyboard.RIGHT )
            {
                rightKeyIsBeingPressed = false;
            }
            else if (keyboardEvent.keyCode ==Keyboard.LEFT )
            {
                leftKeyIsBeingPressed = false;
            }
        }
        }
        public function moveEnemy( timerEvent:TimerEvent ):void 
        {
            for each ( var enemy:Enemy in army ) 
            {

            }
            //enemy.moveDownABit();
            if(enemy.x+enemy.width+2<=gameWidth)
                {
                    enemy.moveRight();
                }
            else if(enemy.y+enemy.height+2<=gameHeight)
                {
                    enemy.moveDown();
                }
            else if(enemy.x-2>=0)
                {
                    enemy.moveLeft();
                }
            else if(enemy.y-2>=0)
                {
                    enemy.moveUp();
                }

        }
        public function moveAvatar( timerEvent:TimerEvent ):void
        {
            if ( useMouseControl )
            {
                avatar.x = mouseX;
                avatar.y = mouseY;
            }
            else if ( rightKeyIsBeingPressed )
                {
                    avatar.moveRight();
                }
            else if ( leftKeyIsBeingPressed )
                {
                    avatar.moveLeft();
                }
        }



    }
}

Enemy Class

package 
{
    import flash.display.MovieClip;
    public class Enemy extends MovieClip 
    {
        public function Enemy( startX:Number, startY:Number ) 
        {
            x = startX;
            y = startY;
        }


        public function moveRight():void
        {
            x = x + 2;
        }

        public function moveDown():void
        {
            y = y + 2;
        }

        public function moveLeft():void
        {
            x = x - 2;
        }

        public function moveUp():void
        {
            y = y - 2;
        }
    }
}

Solution

  • I would suggest that during the enemy instantiation (of which there seems to be only one), you should be setting the locations respective of one another.

    as an example:

    your code currently does this:

    var newEnemy = new Enemy( 60, 30 );
    army.push( newEnemy );
    addChild( newEnemy );
    

    Perhaps it should do something like this:

    for(var x:int = 0; x< ARMY_WIDTH; x++)
    {
        for(var y:int = 0; y < ARMY_HEIGHT; y++)
        {
            army.push(new Enemy(60 + x * X_GAP, 30 + y *Y_GAP));
            this.addChild(army[army.length-1]);
        }
    }
    

    Now, I've made a lot of assumptions in the above code; I've invented constants that you may not have, and I've only solved your immediate problem. Ideally you're going to need to look at more advanced solutions for your issue, something like a factory class that creates your enemies for you, or recycles them from a pool of enemies.

    However, you're just starting out and you will figure a lot of this stuff out in time. Good luck in your project and I hope you keep up developing your skills :)