Game Development Reference
In-Depth Information
object to hold this canvas. This type of blitting operation requires the developer to pre-render all
of the display assets and animations in tile sheets, or arrays of
BitmapData
before they can be
displayed on the screen.
Individual object blits
Individual object blitting refers to using multiple display objects to render the game, each with its
own canvas for displaying tile sheet or array based
BitmapData
objects. For example, if we
copyPixels
a 32
32 tile from our tile sheet to the
BitmapData
attribute of a
Bitmap
object in
Flash, we are blitting from a tile sheet. If we add that
Bitmap
to a
Sprite
object and position it at
an x location of
(width*.5)*-1
and y at
(height*.5)*-1
, then we have our 32
32 tile with the
registration point in the center. Now, we can rotate, scale, and move that individual
Sprite
all we
want. If we want to change its tile, we copy new pixels to the
BitmapData
attribute of the
Bitmap
.
We do this individually for our game characters to reap the benefits of tile sheets and the benefits
of the Flash
Sprite
at the same time.
Combining types of blits
I find that using a combination of the two blitting methods is the most flexible. I have created
entire games with individual blit objects, and I have created entire games with a single canvas
display object. Both work fine, but the game we are going to create in this chapter and the next
one uses a combination of both techniques. We will be using a screen-sized single canvas for the
level map. This will contain only the
TILE_WALL
and
TILE_MOVE
designated tiles. We will then
create our own special class called
BlitSprite
to be used for the sprites in the game. This class
will extend
Sprite
but use our
TileSheet
class to blit animations to the sprite. This structure will
give us the flexibility to use the rotation property of the
Sprite
class and not have to prerender all
of the rotations for each of the eight frames of out tanks.
Testing blitting render speed
Many people have asked us why we like to use blitting operations for animation and screen
rendering. They point out that they are much more difficult than timeline-based animations and
take more time to code and set up. Our answer is
power
. In 2008, We decided to answer those
critics by doing a series of render tests on our web site,
www.8bitrocket.com
. We are not going to
go into the full detail of the tests here, but we will show the results, which indicated a huge speed
and frame rate performance gain by using some type of blitting (individual sprite or full screen)
over using the go-to-and-stop method of timeline-style of animating
MovieClip
objects.
Each test was run by attempting to place increasingly more and more objects on the screen and
timing the average frame rate while doing so. I did the test using 100, 500, 1,000, 5,000, 10,000,
and finally 15,000 objects on the screen. All tests were completed inside the Flash IDE with the
stage frame rate set to 120.
Testing the timeline-based method
This test used individual
MovieClip
objects. I put each tile of my animation on a separate frame of
a
MovieClip
; see Table 6-1.