Game Development Reference
In-Depth Information
All obstacles move from right to left at the same speed (
obstacleSpeed
) to create the illusion that
the player is scrolling from left to right. Every 10 seconds the speed is increased by 1 until it
reaches
obstacleSpeedMax
.
private var obstacleSpeed:int = 6;
private var obstacleSpeedMax:int = 12;
Every 10 seconds the
obstacleColorIndex
will increase by 1. This will change the color of the
obstacles created from the pool to coincide with the color value in the
aObstacleColor
array.
private var obstacleColors:Array = [0xffffff, 0xff0000, 0x00ff00, 0x0000ff, 0x00ffff,
0xffff00, 0xffaaff, 0xaaff99, 0xcc6600];
private var obstacleColorIndex:int = 0;
Ending the game
The game is over then the player hits an obstacle object. We will use standard
hitTestObject
collision detection for this because of the nature of the obstacles. Each obstacle is only a 1
1-pixel
BitmapData
element that we have scaled in code to create various objects. If we tried to use
BitmapData.hitTest
, we would find that the collisions would not be detected properly because
BitmapData.hitTest
works only with the pixels in the original
BitmapData
, not the stretched version
we created by simply setting he
scaleX
and
scaleY
of the
Bitmap
holder for the
BitmapData
object.
When the game is over we want to set the
lastScore
variable for
Main.as
to submit to the Mochi
leader board for the game. When the
checkForEndGame
function detects that the game is over
(
gameOver == true
), it calls
switchSystemState
setting the new state to
STATE_SYSTEM_PLAYER_EXPLODE
.
The
playerExplodeComplete
function actually sets the
lastScore
variable.
public function checkforEndGame():void {
if (gameOver ) {
playerStarted = false;
switchSystemState(STATE_SYSTEM_PLAYER_EXPLODE);
dispatchEvent(new CustomEventSound(CustomEventSound.PLAY_SOUND,
Main.SOUND_EXPLODE,false, 1, 8, 1));
}
}
The playerExplode function is called until the the player object has faded out.
private function systemPlayerExplode(timeDifference:Number=0):void {
playerSprite.alpha -=.005;
if (playerSprite.alpha <= 0) {
playerExplodeComplete();
}
}
This function, in turn, calls
playerExplodeComplete
function when the fade out is finished. This
sets the lastScore variable (inherited by TunnelPanic from Game as you will see in the next
section). This is the same variable that Main will use to set the score in the Mochi leader board.
private function playerExplodeComplete():void {
dispatchEvent(new Event(GAME_OVER) );
lastScore = score;
trace("lastScore=" + lastScore);
disposeAll();
}