Game Development Reference
In-Depth Information
one,” because the total amount of objects at any one time will always be constant rather than
fluctuating wildly at the whims of the garbage collection process, which would be the case if we
created local objects on each frame tick.
Throughout this topic, we have created quite a few custom
Event
calls for each
ScoreBoard
class
element update. In Blaster Mines, we will create a single reusable event for each of these update
events. Let's take a closer look at what we mean now.
Reusing global event objects
The
BlasterMines.as
class sends off a lot of events to update
ScoreBoard
on each frame tick. We
are going to reduce the overall memory footprint, as well as processor and execution time, by
reusing the events that fire off to update each of the
ScoreBoard
elements.
We will take the
score
element as an example, but this will apply to all
ScoreBoard
display
elements in our
Game
class:
1.
First, we need to create a reusable object event for the score update:
private var customcoreBoardEventScore:CustomEventScoreBoardUpdate = new
CustomEventScoreBoardUpdate(CustomEventScoreBoardUpdate.
UPDATE_TEXT,Main.SCORE_BOARD_SCORE, "");
2.
When we make a call to update the
Main.SCORE_BOARD_SCORE
text element on the
score
, we need to set the
value
property of the event to the current
score
value:
customScoreBoardEventScore.value = score.toString()
3.
Finally, we need to dispatch the event:
dispatchEvent(customScoreBoardEventScore);
As you can see, we are simply going to reuse the same class level event variable every time we
update the score and create a new event each time.
Optimizing with look-up tables
Look-up tables can be a very easy source of optimization when calculating complex mathematical
values. It is somewhat debatable whether or not AS3 is faster at math than at array look-ups, but by
using the
FrameCounter
, we were able to see a small difference of 1 FPS when using the single
look-up table that we will use for rotational radian vector values. If applied to a much larger
application, you might see a much greater improvement. We will use the
Vector
class for our look-
up table, which in our experience allows even faster array look-ups. If you do not have access to a
Flash Player 10-compatible publishing system, you can swap the
Vector
for an
Array
instance.
We are already going to use the
BlitArrayAsset
class to create rotational arrays of 360 images
for our player's ship. Because of this, we will be able to use the same frame attribute of the
BasicArrayBlitObject
class to pick out the vector values needed to more our player in the
direction is it facing.