Game Development Reference
In-Depth Information
Adding circles to the screen
If the number of on-screen Circle objects (both blue and red) is less than maxCirclesOnScreen
variable and if the number of blue circles created ( numCreated ) so far on the level is less than the
number of blue circles for the level ( numCircles ), a new Circle object is created.
If a Circle instance needs to be created, a random number from 0 to 99 is created and checked
against the percentBadCircles variable. If the random number is less than or equal to the
percentBadCircles variable, a red circle is created. If not, a blue circle is created.
The call to the new Circle constructor (a custom class we will look at as the final part of this
chapter) takes a single integer parameter. Two static constants are defined in the Circle class:
CIRCLE_BAD and CIRCLE_GOOD . Passing CIRCLE_BAD tells the Circle constructor to create a bad,
red circle. Conversely, CIRCLE_GOOD tells the Circle constructor to create a good, blue circle. If a
good circle is created, the numCreated variable is updated by 1. The newly created Circle
instance is placed in a tempCircle variable, added to the circles array, and added to display list
of Super Click with the addChil function call.
Updating the on-screen circles
After adding (or not adding) a new Circle class instance, the update function loops through all of
the circles currently on the screen. It employs the use of the tempCircle class variable to hold
the current Circle instance in the circles array. The tempCircle size is updated by calling its
update function passing in the circleGrowthSpeed variable. The tempCircle instance will use this
variable to increase the scale of tempCircle . After the update, it checks to see if the tempCircle
has reached its maximum size and removes it from the screen if it has. We will examine the
Circle class in detail in a later section.
If a Circle instance is to be removed, we call the removeCircle function and pass in the current
counter.
Updating the ScoreTextField instances
The ScoreTextField instances will be placed on the screen when the player successfully clicks
on blue circles. The ScoreTextField instances will display the click score for a short period of
time ( life ) and then be removed from the screen. Much like the circles array, we will loop
through the scoreTexts array placing a reference to the current ScoreTextField instance into the
tempScoreText class variable. We will then call the update function of the tempScoreText
reference. A true will be returned from this function of the life for the ScoreTextField is over. If
so, it will be removed from the screen.
Optimizing the loops
For the loop through the Circle instances, we have employed a couple of loop optimizations that
we have found useful over the years. The first optimization is the creation of the circleLength
variable. Before we set up the for loop, we find the length of the circles array and subtract 1
from it. We then use this variable in the loop through the circles array. This does two things.
First, we don't have to keep reevaluating the length of the array on each loop iteration (that takes
up valuable processor time). Also, if we splice an array element from the circles array on one
iteration, and try to reevaluate the circles.length value on the next iteration, we would have
strange runtime errors that would be difficult to diagnose. This is because we might bypass the
final element in the array now that the length is one less than it was when we started.
Search WWH ::




Custom Search