Game Development Reference
In-Depth Information
private function addToScore(scoreAdd:Number):void {
score += scoreAdd;
dispatchEvent(new CustomEventScoreBoardUpdate
(CustomEventScoreBoardUpdate.UPDATE_TEXT,
Main.SCORE_BOARD_SCORE,String(score)));
clicked++;
percent = 100 * (clicked / numCircles);
dispatchEvent(new CustomEventScoreBoardUpdate
(CustomEventScoreBoardUpdate.UPDATE_TEXT,
Main.SCORE_BOARD_PERCENT_ACHIEVED, String(percent)));
dispatchEvent(new CustomEventScoreBoardUpdate
(CustomEventScoreBoardUpdate.UPDATE_TEXT,
Main.SCORE_BOARD_CLICKED,String(clicked + "/" +numCircles)));
}
Once a blue circle has been clicked, this function is called into action. It actually does quite a lot.
First, it must use the passed-in multiplier to add to the current score by casting the probably real
number returned as an integer:
score += int(maxScore * val);
Once the new score value has been calculated, it dispatches a custom
CustomEventScoreBoardUpdate.UPDATE_TEXT
Event
and passes the
Main.SCORE_BOARD_SCORE
as
the key and the value of the
score
variable.
Next, it calculates the new values for the
clicked
and
percent
variables and dispatches custom
CustomEventScoreBoardUpdate.UPDATE_TEXT
events for each. For the
clicked
update, it passes in
the key of
Main.SCORE_BOARD_CLICKED
and the value as a ratio in a
String
that represents the
clicked
variable divided by the number blue circles for the level. Finally, percent achieved on the
scoreBoard
is updated by passing the
Main.SCORE_BOARD_PERCENT_ACHIEVED
key and the current
value of the
percent
variable as a
String
.
Defining the render function
The
render
function simply loops through
Circle
objects and sets
scaleX
and
scaleY
of each to
its
nextScale
value.
private function render():void {
var circleLength:int = circles.length-1;
for (var counter:int = circleLength; counter >= 0; counter--){
tempCircle = circles[counter];
tempCircle.scaleX = tempCircle.nextScale;
tempCircle.scaleY = tempCircle.nextScale;
}
}
The render function employs our loop iteration optimizations, which are a recalculated
circleLength variable and a tempCircle instance, and it iterates backward through the loop. Now,
we didn't necessarily need to loop backward here, because the render loop does not splice any
circle instances from the circles array, but we continued the application this for consistency. It
could very well be coded with a for:each loop.