Skip to main content
Known Participant
January 4, 2014
Question

How to spawn enemies like in space invaders

  • January 4, 2014
  • 1 reply
  • 1238 views

hi

how would i spawn my enemies like in space invaders.

for example in a 2 by 4 rectangle with equal distance apart?

then how would i get them to move left until they hit wall go down one place and then go left and carry on repeating the same thing?

thanks

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
January 4, 2014

you could use code like the following to assign enemies to a grid:

var row:int=2;

var col:int=4;

var gapX:int=15;

var gapY:int=25;

var startX:int=100;

var startY:int=100;

var enemyA:Array=[array of enemies];

for(var xi:int=0;xi<col;xi++){

for(var yi:int=0;yi<row;yi++){

enemyA[xi+yi*col].x=startX+xi*(enemyA[xi+yi*col].width+gapX);

enemyA[xi+yi*col].y=startY+yi*(enemyA[xi+yi*col].height+gapY);

}

}

to move them all use a loop (enterframe or timer) and to check if they hit a wall, use hitTestObject.

Known Participant
January 5, 2014

nothing happens even though there are no errors:

package{

 

          import flash.display.MovieClip;

          import flash.events.Event;

          import flash.utils.Timer;

 

          public class EnemyShip extends MovieClip{

 

                    var speed:Number;

                    static var list:Array = new Array();

                    var shootTimer:Timer;

                                                  var row:int=2;

var col:int=4;

var gapX:int=15;

var gapY:int=25;

var startX:int=100;

var startY:int=100;

var enemyA:Array;

 

                    function EnemyShip(){

 

                    }

 

                    function enterFrame(e:Event){

 

                              this.x -= speed;

                              if(this.x < -100){

                                        kill();

                                        return;

                              }

 

                              if(this.hitTestObject(Game.ship)){

                                        kill();

                              }

 

                    }

 

                    function kill(){

 

                              removeEventListener("enterFrame", enterFrame);

                              stage.removeChild(this);

 

                              for(var i in list){

                                        if(list == this){

                                                  delete list;

                                        }

                              }

 

                    }

 

 

 

          }

 

}

kglad
Community Expert
Community Expert
January 5, 2014

1. you're not using the grid code i suggested

2. that wouldn't be the class where you would add the grid code i suggested.  the grid code should be in the class that creates the enemies.

3. that is the class where you would add the hittestobject code which you're using.