As Conor mentioned in class, it would be good to add a strafing action to the shooter in Space Invaders. To compensate, we'll have to add more invaders as well.
Start by downloading the Space Invaders (invaders.fla) from our class Games directory.
Take a look at the ActionScript that sits behind our first Stage 1 frame:
setupBullet() is the routine called from our initiation function, init();
There is only one bullet being set up in the setupBullet() function:
function setupBullet(){
bullet.dy = 0;
bullet.onEnterFrame = function(){
bullet._y += bullet.dy;
} // end enterFrame
} // end setupBullet
Now, let's replace that one bullet with an array of bullets.
var cur_bullet = 0; var NUM_BULLETS = 10;And we'll re-write the setupBullet() routine to manage multiple bullets instead of one. We'll attach our bullet as a Movie Clip we can reuse in multiple bullet objects (as opposed to adding the bullet to the stage manually with Flash and giving it an instance property):
function setupBullet(){
for (b = 0; b < NUM_BULLETS; b++){
_root.attachMovie("bullet", "bullet_" + b, b*100);
theBullet = eval("bullet_" + b);
theBullet.dy = -1;
//off screen
theBullet._x = -100.0;
theBullet.onEnterFrame = function(){
shootUp();
} // end enterFrame
} //end bullet creation
} // end setupBullet
The eval() function lets us re-use a variable like theBullet with different bullets that we identify by name (in this case a string comprised of the word bullet plus an underscore character _ and the offset in the array. bullet_7 is the name of the eighth (we start counting with 0) bullet in the array of bullets we are managing associated with our simple bullet Movie Clip.
function shootUp(){
//shoot the bullets up
for (b = 0; b < NUM_BULLETS; b++){
theBullet = eval("bullet_" + b);
theBullet._y += theBullet.dy;
} // end for loop
} // end shootUp
The bullets just keep moving upwards one pixels per frame (we set the dy property equal to -1 in the setupBullet() method). If we have not shot the bullet with the space bar, we'll just keep it's _x property to be off stage (anything less than 0 is off stage). To do so, we need to keep track of which bullet we are firing in the SPACE BAR keyboard key check:
if (Key.isDown(Key.SPACE)){
theBullet = eval("bullet_" + cur_bullet);
theBullet._x = ship._x;
theBullet._y = ship._y;
cur_bullet++;
if (cur_bullet >= NUM_BULLETS) {
cur_bullet = 0;
}
} // end if
With the routine, we rotate between our bullets from 0 to 9, firing them all in succession if we wish to with the space bar. We update the cur_bullet global variable each time we shoot a new bullet. When our cur_bullet gets up to the maximum, we reset it to 0 again.
for (b = 0; b < NUM_BULLETS; b++){
theBullet = eval("bullet_" + b);
if (this.hitTest(theBullet)){
this.removeMovieClip(this);
//reset bullet
theBullet._x = -100;
} else {
if (theBullet._y < 0) {
theBullet._x = -100;
}
}
} //end bullet check for loop
Hopefully, you could determine where to replace or add code based on your understanding of the invaders.fla Flash file. Once you have tried your best, take a look at one possible solution by downloading the invaders6.fla file.