Game Development Reference
In-Depth Information
Releasing either the left or right arrow key sets the horizontal velocity to ,—the lh]uan object stops mov-
ing left or right. Because both conditions have exactly the same result, it makes sense to combine them
into one statement with an kn operator. It very efficiently saved a few extra lines of redundant code.
Using the onEnterFrame event handler
The kjAjpanBn]ia event handler is what actually makes the lh]uan object move. It's triggered by an
event listener that uses the AJPAN[BN=IA property of the Arajp class. To use it, you first have to import
the Arajp class with this eilknp directive in the class definition:
eilknpbh]od*arajpo*Arajp7
You then set up the event listener in the ejep method with this directive:
]``ArajpHeopajan$Arajp*AJPAN[BN=IA(kjAjpanBn]ia%7
It follows the same format as the other two listeners, with one important difference: the listener isn't
attached to the op]ca object. So what is it attached to? Adding an event listener without attaching
to an object means that the listener is attached directly to the actual class it's in ; in this case, the
I]ej[?d]n]_pan[Psk class. This won't be of much relevance now, but it will become very important
when you start looking at building games using different classes in Chapter 8.
When the event listener is triggered, it calls the kjAjpanBn]ia event handler:
bqj_pekjkjAjpanBn]ia$arajp6Arajp%6rke`
w
++Ikrapdalh]uan
lh]uan*t'9rt7
lh]uan*u'9ru7
y
What this event handler does is very simple: it takes the horizontal and vertical velocities in the rt and ru
variables and assigns them to the lh]uan object's t and u properties. That makes the object move. Yay!
But wait. What is the event that calls this handler? You know that other events in the program are triggered
by keys being pressed or released. Gau^k]n`Arajp*GAU[@KSJ and Gau^k]n`Arajp*GAU[QL are pretty self-
explanatory in that regard, but what kind of event is Arajp*AJPAN[BN=IA?
Put the kettle on and throw another log on the fire. Here's a little story that might help explain what's
going on. Flash was originally designed as a tool for doing animation, which is the art of creating the
illusion of motion from nonmoving objects. A lot of the concepts that Flash borrowed came from the
animation industry, which you might know used celluloid film to create this illusion. Here, briefly, is
how animation with film works:
Film is a long strip of celluloid (plastic) made up of a series of little squares called frames . Each
frame contains an image, and each image in a frame is just slightly different from the image that's
in the frame that comes before it. If enough of these slightly different images are flashed in front of
a viewer's eyes fast enough, the individual nonmoving images will appear to be a single image that
moves. This illusion of motion, which is the basis of all film, video, animation, and even game anima-
tion, is called persistence of vision .
 
Search Nedrilad ::




Custom Search