Game Development Reference
In-Depth Information
We also need to test the player against the enemies. This test is much like the test we made in
Balloon Saw with balloons against the player. The difference is that in Balloon Saw, the player
scored points in this loop. In Pixel Shooter, the player's ship is destroyed. We need to handle that
event by decrementing the chances variable and setting the gameState to
STATE_REMOVE_PLAYER.
for (i=enemies.length-1;i>=0;i--) {
tempEnemy = enemies[i];
if (tempEnemy.hitTestObject(player)) {
chances--;
chancesText.text = chances.toString();
makeExplosion(tempEnemy.x, tempEnemy.y);
gameState = STATE_REMOVE_PLAYER;
}
}
}
Creating explosions
The last bit of interesting code we will look at in Pixel Shooter is the makeExplosions() function.
makeExplosions() takes x and y positions as its two parameters and creates an instance of
ExplosionImage . It also plays the Explode sound by creating an instance of that object from the
library and calling its play() method. Other than those two things, creating an explosion is much
like putting any other object on the screen. The main difference is that we do not set a speed
variable, because explosions don't move. However, the explosion will animate (recall, it has
seven frames to run through). The MovieClip will play automatically when it is created, so all we
have to do is put it on the screen using addChild() and push it into the explosions array so we
can manage it later.
public function makeExplosion(ex:Number, ey:Number) {
var tempExplosion:MovieClip;
tempExplosion = new ExplosionImage();
tempExplosion.x = ex;
tempExplosion.y = ey;
addChild(tempExplosion);
explosions.push(tempExplosion);
var sound:Sound = new Explode();
sound.play();
}
There is a bit more code that we have changed to iterate Balloon Saw into Pixel Shooter, but you
should get the idea. The point we trying to illustrate is this: by using the code from a completed
game as the starting point, you can get a jump start on your development of another game. We
took the code from the click game and added functionality to create Balloon Saw. We then added
more functionality and changes to morph it into Pixel Shooter. In theory, you could easily take the
code from Pixel Shooter and make an even more elaborate game, using the same iterative
concepts.
Search WWH ::




Custom Search