Game Development Reference
In-Depth Information
The choice of the variable names rt and ru has become a programming convention
for variables that refer to horizontal and vertical velocities, so they're used in this topic.
Most programmers who see the variable names rt and ru immediately know what
they're referring to. It's just one of those unwritten programming rules that everyone
seems to follow, and no one knows why. Of course, you can give these variables any
name you like, such as rahk_epuT and rahk_epuU (which are much more descriptive
and might make your program easier to read). But, hey, conventions are sometimes a
hard thing to knock, so this topic sticks with rt and ru .
Using the new onKeyDown event handler
The kjGau@ksj event handler has changed slightly from the first program:
bqj_pekjkjGau@ksj$arajp6Gau^k]n`Arajp%6rke`
w
eb$arajp*gau?k`a99Gau^k]n`*HABP%
w
rt9)17
y
ahoaeb$arajp*gau?k`a99Gau^k]n`*NECDP%
w
rt917
y
ahoaeb$arajp*gau?k`a99Gau^k]n`*QL%
w
ru9)17
y
ahoaeb$arajp*gau?k`a99Gau^k]n`*@KSJ%
w
ru917
y
y
The eb+ahoa statement is no longer changing the lh]uan object's t or u properties directly. Instead, it's
simply updating the rt and ru variables with the appropriate velocity. The job of actually moving the
object is delegated to the kjAjpanBn]ia event handler. More on that in a moment, but first let's take
a quick look at this other new event handler: kjGauQl.
Using the onKeyUp event handler
In the first simple keyboard control program, an event handler called kjGau@ksj that figures out which
keys are being pressed was added to the stage. The funny thing about the kjGau@ksj event handler is that
it only knows when keys are being pressed down, not when they're released. It turns out that knowing that
a key is up is just as important for games as knowing that it's down. AS3.0 therefore has a handy little prop-
erty of the Gau^k]n`Arajp class called GAU[QL that can tell you this information. In the ejep method, you
used the GAU[QL property in an event listener that you attached to the op]ca object in this directive:
op]ca*]``ArajpHeopajan$Gau^k]n`Arajp*GAU[QL(kjGauQl%7
 
Search Nedrilad ::




Custom Search