Game Development Reference
In-Depth Information
Like the removeCircle function, we remove
ScoreTextField
instances with three separate
function calls. First, we simply call the
ScoreTextField
object's internal
dispose
function. We will
see this function when we discuss the
ScoreTextField
class. The internal
dispose
function should
set to
null
all internal objects and get rid of all event listeners.
Next, we simply remove the
ScoreTextField
from the
SuperClick
display list with the
removeChild
function call. The last thing we do is to splice the
ScoreTextField
instance from the
scoreTexts
array.
Defining the checkCollisions function
The
checkCollisions
function is called every frame tick from the
SuperClick
class
runGame
function inside the
SYSTYEM_STATE_GAMEPLAY
state of the
Main
class. It doesn't actually check real
collisions between objects or the mouse pointer colliding with a
Circle
instance. It does loop
through the
Circle
objects in
circles
and checks for instances that have their
clicked
variable
set to
true
.
private function checkCollisions():void {
var circleLength:int = circles.length-1;
for (var counter:int = circleLength; counter >= 0; counter--){
tempCircle = circles[counter];
if (tempCircle.clicked && !tempCircle.fadingOut) {
tempCircle.fadingOut = true;
if (tempCircle.type==Circle.CIRCLE_GOOD &&
tempCircle.alpha==1) {
var scoreAdjust:Number = 1 / tempCircle.scaleX;
var scoreAdd:int=maxScore * scoreAdjust;
addToScore(scoreAdd);
tempScoreText = new ScoreTextField(String
(scoreAdd), textFormat, tempCircle.x,
tempCircle.y, 20);
scoreTexts.push(tempScoreText);
addChild(tempScoreText);
}else if (tempCircle.type==Circle.CIRCLE_BAD) {
gameOver = true;
}
}
}
}
The
checkCollisions
function continues our loop optimizations by using the
circleLength
local
variable to hold the length of the
circles.length-1
value. It also employs the
tempCircle
class
reference variable and loops through the
circles
array backward. The loop's job is to check the
current
tempCircle
instance in the iteration for a
true
value in its
clicked
variable. If the variable
is set to
true
, the function removes it, calculates the new score (if blue), and disposes of it. If a
red
Circle
was clicked, the function sets
gameOver
to
true
.
The call to the internal
addToScore
function will be discussed next, but the call takes a single
parameter that represents the value to multiply the
maxScore
variable by when adding to the
player's
score
.
This value is calculated in a way that gives the player a higher score the smaller
the circle that is clicked. The multiplier is
1/Circle.scaleX
. So, if the
scaleX
of the circle is
.7
, the
value passed in is 1 divided .7, or about 1.4 times the maximum score. At a scale of
1
, the