Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3 game - How to spawn enemies from left and right side of the screen?

My character is spawned in the middle of the screen, and i'm not sure how to make zombies spawn from the left and right side of the screen, and moving towards the middle. My goal is to make the zombies spawn automatically, and the character in the middle is able to shoot them down. I've just started working on AS3, but i know that i'll need to make an array to do this, but not sure how to implement this. Can someone show me? Thanks. Here is my code:

package 
{
    import flash.display.MovieClip;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import flash.events.KeyboardEvent;
    import flash.events.Event;
    import flash.ui.Keyboard;
    public class ActualGame extends MovieClip
    {
        public var char:Character;
        public var zombie:Zombieclass;
        public var timer:Timer;
        private var speed:Number = 4;
        private var moveleft:Boolean = false;
        private var moveright:Boolean = false;
        private var i:Number =0;

        public function ActualGame()
        {
            char = new Character();
            stage.addChild(char);
            zombie = new Zombieclass(0,360);
            addChild(zombie);
            timer = new Timer(25);
            timer.addEventListener(TimerEvent.TIMER, zombie_spawn);
            stage.addEventListener(Event.ENTER_FRAME, enterframe);
            stage.addEventListener(KeyboardEvent.KEY_UP, keyup);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, keydown);
            timer.start();
        }

        public function zombie_spawn(timerEvent:TimerEvent):void
        {
            zombie.zombie_move_right();
            if (char.hitTestObject(zombie))
            {
                timer.stop();
                trace("hit");
            }
        }
        public function enterframe(event:Event)
        {
            if (moveleft)
            {
                char.x -=  speed;
                char.scaleX = -1;
            }
            else if (moveright)
            {
                char.x +=  speed;
                char.scaleX = 1;
            }
        }
        public function keydown(event:KeyboardEvent)
        {
            switch ( event.keyCode )
            {
                case Keyboard.LEFT :
                    moveleft = true;
                    break;
                case Keyboard.RIGHT :
                    moveright = true;
                    break;
            }
        }
        public function keyup(event:KeyboardEvent)
        {
            switch ( event.keyCode )
            {
                case Keyboard.LEFT :
                    moveleft = false;
                    break;
                case Keyboard.RIGHT :
                    moveright = false;
                    break;
            }
        }
    }
}

Zombieclass:

package 
{
    import flash.display.MovieClip;
    public class Zombieclass extends MovieClip
    {

        public function Zombieclass(xcoord:Number, ycoord:Number)
        {
            x = xcoord;
            y = ycoord;
        }
        public function zombie_move_right():void
        {
            x +=  1.5;

        }
        public function zombie_move_left():void
        {
            x -=  1.5;
        }
    }

}
like image 980
Vincent Lam Avatar asked Dec 27 '25 14:12

Vincent Lam


1 Answers

I understand your problems, and I have been there myself. When just learning to code by trying, copying other code and modifying it, and eventually complpeting your goals you might miss some essentials that need to be learned later.

An Array works sort of like stack of paper. You can put a sheet of paper into it, you can one out, or you can put the whole stack on your desk and put a stamp on all of them, one by one. This is what you want to do with your zombies. (Figuratively speaking).

You will also need to learn the concept of loops. Using loops you can manipulate all the objects in an array with very few lines of code, even if you do not know beforehand how many items are in you array.

You will want to do the following:

  • Create an array to keep your zombies in.
  • Fill your array with zombies.
  • Loop through your zombies every frames, telling them to move.

Create your array like this: public var zombies:Array = new Array();

Add some zombies:

var numberOfZombies:uint = 10;

for(var i:uint = 0; i<numberOfZombies; ++i) {
   var zombie:ZombieClass = new ZombieClass(0, (360/numberOfZombies)*i);
   addChild(zombie);

   zombies.push( zombie );
}

This will create 10 zombies, put them on the screen, and add them to the array.

Then, on every frame, you can loop through the array and move the zombies:

for(var i:uint = 0; i<numberOfZombies; ++i) {
    var zombie:ZombieClass = zombies[i];

    zombie.zombie_move_right();
}

Or something. This code probably contains some errors, since it is not tested and I wrote it on my iPad, but hopefully it will help you a bit further. Learn to use arrays and loops.

Good luck.

like image 87
Simen Øian Gjermundsen Avatar answered Dec 30 '25 17:12

Simen Øian Gjermundsen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!