Game Development Reference
In-Depth Information
It then becomes the job of kjAjpanBn]ia to move the object. Don't worry too much about the
complex-looking code that it contains. Most of that code is there to solve a small technical problem
that you have to deal with whenever you use easing. The essence of the kjAjpanBn]ia event handler,
the code that actually makes the object move, is in these two lines:
lh]uan*t'9\$[p]ncapT)lh]uan*t%&A=OEJC7
lh]uan*u'9\$[p]ncapU)lh]uan*u%&A=OEJC7
These directives use exactly the same formula as that in the drag-and-drop example. In fact, if these
were the only two lines in the kjAjpanBn]ia event handler, the class would still work perfectly well.
What is the rest of the code for? The easing formula invokes another example of Xeno's Paradox, which
I discussed in the previous chapter. The object slows down gradually as it approaches its target, but
although it appears to eventually stop moving, the numbers in the background still try to calculate
ever-smaller divisions of a pixel for it to move toward. As far as AS3.0 is concerned, the final destination
is never actually reached. You might think that this might not matter because you can't see the effect
of it on the stage, but it's not a good idea to allow mathematical processes like that running in the
background—they consume precious CPU power that could used to improve the performance of other
aspects of the game. Preventing this from happening is like sealing a window in a drafty old house.
A good solution is to find out whether the object is less than 1 pixel from its destination. If it is, you
know that the object is close enough, and you can quit the easing motion. One pixel is the smallest
amount visible on the stage, so even if the easing formula is still calculating farther smaller fractions
of a pixel, stopping it at this point won't matter.
But how can you calculate how far the object is from the target destination? You could compare the
absolute values of the t and u positions of the object and its target, as you did in the Bug Catcher
game, to find out how close the bugs were from the cat. However, there's another way:
1. Calculate the distances between the object and the target on the x and y axes, and store these
values as variables called `t and `u (you saw this approach in the collision detection code used
in the ?khheoekj class):
r]n`t6Jqi^an9`aopej]pekjT)knecejT7
r]n`u6Jqi^an9`aopej]pekjT)knecejT7
2. Apply a simple formula, the Pythagorean Theorem, and copy the result into a variable called
`eop]j_a:
r]n`eop]j_a6Jqi^an9I]pd*omnp\$`t&`t'`u&`u%7
The Pythagorean Theorem states that “the square of the hypotenuse of a right triangle is equal to the
sum of the squares on the other two sides.” Translated into practical AS3.0 code, this means you need
to use the built-in I]pd*omnp function to find the square root of the sum of the `t and `u values,
which are then multiplied by each other. Luckily for you, Pythagoras was right! Whenever you need to
find the distance between two points, use his formula. It will become a regular in your arsenal of game
design tricks, and you'll be using it a lot in this chapter.