Game Development Reference
In-Depth Information
exhaustParticles.splice(ctr,1);
}
}
canvasBitmapData.unlock();
playerSprite.y += gravity;
}
private function randomNumberFromRange(min:int, max:int):int {
return(int(Math.random() * (max - min)) + min);
}
As usual, after the update functions come the
collision checking and render functions
. The
collision routines are very simple for Tunnel Panic. We have implemented the standard
hitTestObject
functions. We did this because the
BitmapData
that makes up our obstacles has
been scaled for each obstacle. The
BitmapData.hitTest
functions will not work correctly on
scaled or rotated objects.
private function checkCollisions():void {
var playerHit:Boolean = false;
if (playerSprite.y < playfieldminY+10 || playerSprite.y > playfieldmaxY-10) {
trace("hit outside bounds");
playerHit = true;
}
for each (tempObstacle in obstacles) {
if (playerSprite.hitTestObject(tempObstacle)) {
trace("hit obstacle");
playerHit = true;
}
}
if (playerHit) {
gameOver = true;
}
}
private function render():void {
canvasBitmapData.lock();
for each (tempExhaustParticle in exhaustParticles) {
tempExhaustParticle.render(canvasBitmapData);
}
canvasBitmapData.unlock();
}
Finally, you have these remaining functions for clean up and similar operations:
private function updateScoreBoard():void {
customerScoreBoardEventScore.value = score.toString();
dispatchEvent(customerScoreBoardEventScore);
}
private function disposeAll():void {
//move all obstacles left in active to pool
var obstacleCount:int = obstacles.length - 1;
for (var ctr:int = obstacleCount; ctr >= 0; ctr--) {
tempObstacle = obstacles[ctr];
removeChild(tempObstacle);