Game Development Reference
In-Depth Information
The other optimization is the loop backward through the circles array. By doing this, when we do
splice a Circle instance from the array (because it has reached its maximum size), we won't skip
the Circle instance in the array following the one we splice.
For example, let's say we are looping forward through our circles array and we find that the tenth
element in the array needs to be removed. We will need to call the splice function on the array to
remove it. When we do this, the eleventh Circle instance in the array now becomes the tenth,
and subsequently, all of the elements that follow the eleventh have to shift a place also. Now, on
the next loop iteration, we would bypass the new tenth (which was the eleventh) element and
move on to the new eleventh (the one that was twelfth). We have skipped processing on the
original eleventh element.
We also employ one more optimization. By using the tempCircle and setting it to reference the
current circles[ctr] instance, we don't have to perform a lookup on the circle array on each
Circle instance attribute access call. Array element access is a little slow slow but necessary,
and we need to keep it to a minimum if possible.
Optimization! To optimize a loop, assign the length of your array to a variable and use it in the for
loop instead of the Array.length attribute. Iterate backward through your Array instance if there
is the possibility of removing an element from the array with splicing. Assign the value of your
Array[loop counter] so the lookup does not have to occur more than one time. The lookup is
very slow.
Defining the removeCircle function
The removeCircle function takes in an integer parameter corresponding to the index of the
Circle instance to be removed from the circles array. Its job is to cleanly dispose of Circle
object instances.
private function removeCircle(counter:int):void {
tempCircle = circles[counter];
tempCircle.dispose();
removeChild(tempCircle);
circles.splice(counter, 1);
}
We remove Circle instances with three separate function calls. First, we simply call the Circle
object's internal dispose function. You will see this function when we discuss the Circle class. The
internal dispose function should set to null all internal objects and get rid of all event listeners.
Next, we simply remove the Circle from the SuperClick class display list with the removeChild
function call. The last thing we do is to splice the Circle instance from the circles array.
Defining the removeScoreText function
The removeScoreText function takes in an integer parameter corresponding to the index of the
ScoreTextField instance to be removed from the scoreTexts array. Its job is to cleanly disposes
of ScoreTextField object instances.
private function removeScoreText(counter:int):void {
tempScoreText = scoreTexts[counter];
tempScoreText.dispose();
removeChild(tempScoreText);
scoreTexts.splice(counter, 1);
}
Search WWH ::




Custom Search