Game Development Reference
In-Depth Information
if (doCopyPixels) {
bitmap.bitmapData.lock();
rect.x=int(tileList[currentTile] % tileSheet.tilesPerRow)*tileWidth;
rect.y=int(tileList[currentTile] / tileSheet.tilesPerRow)*tileHeight;
bitmap.bitmapData.copyPixels(tileSheet.sourceBitmapData, rect, point);
bitmap.bitmapData.unlock();
}
doCopyPixels = false;
}
public function dispose():void {
bitmap.bitmapData.dispose();
bitmap = null;
rect = null;
point = null;
tileList = null;
}
}
}
The
BlitSprite
class is a basic building block of all of the game characters in the No Tanks!
game. It is an extension of the built-in
Sprite
class, so we can utilize rotation and other features
easily. It doesn't have a timeline, so we build our own way of animating characters by passing in
an instance of our
TileSheet
class along with an array of tile
id
s and the
id
of the first tile to
display in the list of tile
id
s. These tile
id
s will be used to reference tiles on the 8
4-tile sheet we
created in Chapter 6. These tiles will be applied to the
Bitmap
object inside our
BlitSprite
instances by using the
copyPixels
method.
Animating with the BlitSprite class
We don't have the space to go into all the details (line by line) of the
BlitSprite
class so here are
the essentials:
The
BlitSprite
class requires a
TileSheet
instance passed to it. This will be stored in a
private variable called
tileSheet
.
It also requires an
Array
instance passed to it representing the list of tile sheet
id
s (a
single-dimensional list in an array) that represent the sequence of frames for its
animation. This is stored in the
tileList
array variable.
It requires a frame value (
firstFrame
) passed to it that will be used to seed the
currentTile
public property.
It contains a
Bitmap
(named
bitmap
) object that is centered on the origin of the
Sprite
(-
.
5* width, -.5*height
). This enables the
BlitSprite
to rotate the
Bitmap
on its center
axis when the
BlitSprite
itself is rotated.
The
Bitmap
contains a
BitmapData
instance (named
bitmapData
) that will be used for
blitting. Each time the look of the
BlitSprite
is to change to a new tile, the pixels are
copied to the
BitmapData
from the
TileSheet
.