Game Development Reference
In-Depth Information
Next, we create an instance of PlayerImage() from the library (this is the saw blade graphic), and
add it to the screen using addChild(player) . Since the saw blade will be controlled with the
mouse, we will do this the simplest way possible: having the mouse drag the saw around the
screen by attaching player to the mouse. We do this with the player.startDrag() function, which
accepts two parameters:
lockCenter : This object is locked to center of the mouse cursor. We want this, so we set
it to true .
bounds : This defines a Rectangle (the flash.geom.Rectangle class) that specifies where
the mouse can go on the screen. This parameter is not required. However, we have
added it, but we have set it to the full bounds for the screen. Why? Because it might be
interesting to tweak the bounds in this game to make it work more like a traditional catch
game where the player is restricted to x movement only. You could do this by setting the
bounds to 0,0,550,0 .
Last, we create the enemies array that will hold the balloons ( EnemyImage ) objects that the saw will
try to pop.
public function initGame() {
score = 0;
chances = 0;
level = 1;
levelText.text = level.toString();
player = new PlayerImage();
addChild(player);
player.startDrag(true,new Rectangle(0,0,550,400));
enemies = new Array();
gameState = STATE_PLAY;
}
The playGame() function
The playGame() function is called from the gameLoop() .It is the workhorse gameState for Balloon
Saw. The first thing this function does is update the rotation of player 15 degrees. This makes
the player's saw look like it is rotating—just like a real saw blade. Setting the rotation property of
a MovieClip is very handy way to make objects appear to spin in AS3. rotation is set in degrees.
A positive number rotates clockwise, a negative number, counter-clockwise.
The next call to makeEnemies() checks to see if an enemy should be created, and if so, put it on
the screen. moveEnemies() updates the y location for the balloons and removes them if they have
left the screen. The testCollisions() functions checks to see if the player's saw is touching any
objects in the enemies array and, if so, handles that event. Finally, testForEnd() tests to see if the
level needs to be increased or the game needs to end. All of these functions will be described in
detail after the code.
public function playGame() {
player.rotation += 15;
makeEnemies();
moveEnemies();
testCollisions();
testForEnd();
}
Search WWH ::




Custom Search