Game Development Reference
In-Depth Information
removePlayer();
break;
case STATE_END_GAME:
break;
}
}
The startPlayer() function adds the player object to the screen with addChild() and then attaches
it to the mouse using startDrag(). The main difference here is that the box that the player must
stay within is at the bottom of the screen, like a traditional top-down shooter.
public function startPlayer() {
addChild(player);
player.startDrag(true,new Rectangle(0,365 ,550,365));
gameState = STATE_PLAY_GAME;
}
The removePlayer() function actually does a bit more than you might think. Since we will be
calling removePlayer() when the player's ship has been hit by an alien or when the game is over,
we can also remove all the enemies, missiles, and explosions on the screen. This will make it
appear like the level is resetting. One drawback to this is that while we remove the player from
the screen, we then call startPlayer() (by setting the gameState to STATE_START_PLAYER)
immediately, which will put the player back on the screen. We don't have time to show an
explosion or make it look like the player has left the playing field. While this limitation is OK for
this little game, it's really not an elegant way to show the player the intended result. Later in this
topic, we will discuss strategies around creating these nuances that will make your games seem
much more like polished products.
public function removePlayer() {
for(var i:int = enemies.length-1; i >= 0; i--) {
removeEnemy(i);
}
for(i = missiles.length-1; i >= 0; i--) {
removeMissile(i);
}
removeChild(player);
gameState = STATE_START_PLAYER;
}
Tracking multiple objects
In Balloon Saw, we only had to move one type of object, the enemy balloons. In Pixel Shooter,
we need to move three. Other than that, the code should look pretty familiar. The enemies now
move down, so we update each y value by adding the value of speed to it, and we remove the
enemy when it reaches the bottom of the screen. Missiles on the other hand, move almost exactly
like the balloons from Balloon Saw: they move up the screen and are removed when they reach
the top.
The explosions though, work a bit differently. Recall that the explosion_image MovieClip from the
library has seven frames in it, with a stop(); in the last frame. Since explosions don't move, the
only way we know when to remove them is when they have reached their last frame. Since every
MovieClip has a property of currentFrame and totalFrames (the last frame) that you can test, we
can do this with this line of code:
Search WWH ::




Custom Search