Game Development Reference
In-Depth Information
The No Tanks! game class will call the
updateCurrentTile
function of the
BlitSprite
class on each frame tick.
The
animationLoop
Boolean controls whether or not the
BlitSprite
should spend any
cycles working to find a new tile for the
Sprite
. The
Player
and
Enemy
tanks don't want
to animate their respective tank tracks when they are stationary, so in that instance, this
variable will be set to
false
.
The
animationCount
is used to count frames until the count is greater than the
animationDelay
variable
.
If it is greater, the
currentTile
value is updated to the next
value in the
tileList
array. In this case, the
doCopyPixels
public attribute is set to
true
.
The
renderCurrentTile
function is used to copy the new set of pixels to the
BitmapData
inside the
Bitmap
object. The No Tanks! game class will call this on each frame tick. If
true
is passed in, it will force the function to copy the current tile from the tile sheet.
Next, the
doCopyPixels
Boolean value is evaluated. This is set in the
updateCurrentTile
function (or passed in as
true
). If it is
true
, then tile sheet pixels are copied from the
tilesheet
to the
bitmapData
inside the
bitmap
instance.
We set the
bitmapData.lock
before the
copyPixels
and
bitmapData.unlock()
after. This
is an optimization that will help with rendering speed. Essentially, when you do this, you
are telling all objects that have the
BitmapData
instance as an attribute reference to
not
update themselves until the
unlock
is set. This smoothes out the animation and allows
for the processor to not have to keep up with the
copyPixels
operation on all objects
while the copy is being carried being carried out. After the
unlock
call, all of the pixel
data is copied to the display objects.
The
dispose
function is used to clean up the memory used by this object after it is
deleted from the screen.
The TileByTileBlitSprite class
All of our game objects will use the
BlitSprite
class as a base class. Some of them though (the
player and the enemy tanks) will use another class that is a subclass of the
BlitSprite
class.
The
TileByTileBlitSprite
class is simply an extension of the
BlitSprite
class that contains
some variables necessary for moving about the maze in a logical manner.
We will create the class as part of the reusable framework that was started in Chapter 2. The
locations for the file in the framework will be as follows:
/source/classes/com/efg/framework/TileByTileBlitSprite.as
Here is the complete code for the class:
package com.efg.framework
{
/**
* ...
* @author Jeff Fulton
*/