Game Development Reference
In-Depth Information
and y coordinates of the top-left corner of the
Rectangle
that represents the
scrollRect
for the
Bitmap
. In Chapter 10, we used a
copyPixels
offset to scroll the viewable window about the world
canvas. We noticed a 2 to 3 frames per second increase in speed using the
scrollRect
method
over the
copyPixels
method when testing it with Blaster Mines.
This method also greatly simplifies and reduces the number of operations we have to go through
to scroll our screen. In Blaster Mines, the viewable screen is 400
400 while the entire world is
800
800. We are not using tiles as in Drive She Said, so we don't need a buffer with extra tiles
to ensure smooth scrolling. All we really need to do is this:
1.
Update the player's
nextX
and
nextY
values based on the position of the mouse.
2.
800
canvasBitmapData
world at its current position.
Its
x
and
y
coordinates are updated to be the same as its
nextX
and
nextY
coordinates
The player is rendered to the 800
3.
The top-left corner of the
canvasBitmap
(the display object holder of our
canvasBitmapData
blit canvas) is set to be
player.x-200, player.y-200
. The size is
always set to 400
400. This sets our player in the middle of the screen.
4.
When the player moves, we simply change the
x
and
y
coordinates of the upper corner
of this
scrollRect
Rectangle
. This will simulate scrolling over the entire world:
if (playerStarted) {
canvasRect.x = player.x - 200;
canvasRect.y = player.y - 200;
if (canvasRect.x < 0) canvasRect.x = 0;
if (canvasRect.y < 0) canvasRect.y = 0;
if (canvasRect.x > 399) canvasRect.x = 399;
if (canvasRect.y > 399) canvasRect.y = 399;
canvasBitmap.scrollRect = canvasRect;
}
To ensure that the canvasBitmap.scrollRect does not go outside the boundaries of our world
canvasBitmapDdata, we make sure to keep the upper left-hand corner between 0 and 399 for
both the x and y directions.
Notice that we update a
Rectangle
instance called
canvasRect
rather than the
canvasBitmap.scrollRect
directly. The properties of the
scrollRect
cannot be changed directly.
You must change the properties of a second
Rectangle
instance and then assign the
canvasBitmap.scrollrect
to equal that
canavsRect
(the last line of the code).
This simple scrolling technique is very powerful and, as we said, could save 2 to 3 frames per
second over using the camera buffer method from Chapter 10.
Optimizing BitmapData reuse for the radar screen
The Blaster Mines game contains an entire miniature version of the game world to the right of the
gameplay screen. This was actually very simple to create using a full-screen blitting technique.
First, let's discuss a traditional Flash method of scrolling the screen and creating an associated
radar style screen. If we had chosen to use a
Sprite
canvas rather than a blit canvas, we would
have a
Sprite
holder for our game screen with hundreds of other individual
Sprites
(or
Bitmap
objects) attached to its display list.