Game Development Reference
In-Depth Information
3. Use the keyboard arrow keys to move the lh]uan object around the stage. It works! (If it
doesn't, double-check to make sure that you assigned I]ej[?d]n]_pan as the document class
and gave the player character the instance name lh]uan.)
I'm sure you noticed some obvious problems with this player character control scheme already, but
before I show you the solution, let's have a quick look at how it is working. The first thing you had to
do was import two new classes into the program:
eilknpbh]od*arajpo*Gau^k]n`Arajp7
eilknpbh]od*qe*Gau^k]n`7
The Gau^k]n` class's primary job is to make it easier to figure out which keys the player of the game
is pressing. The Gau^k]n`Arajp class allows you to add an event listener to the stage to listen for key
presses. That's exactly what this next bit of code in bold in the ejep method does:
lner]pabqj_pekjejep$%6rke`
w
op]ca*]``ArajpHeopajan$Gau^k]n`Arajp*GAU[@KSJ(kjGau@ksj%7
y
AS3.0 has a built-in class called Op]ca. When you publish a SWF file by testing the project, AS3.0
automatically generates an instance of this class called op]ca (with a lowercase s ,) and you can use
this op]ca object anywhere in your program. This object is very useful for attaching keyboard event
listeners, and that's what you've done here.
You added an event listener to the op]ca object, which is triggered by the GAU[@KSJ property of the
Gau^k]n`Arajp class. The job of Gau^k]n`Arajp*GAU[@KSJ is to listen for key presses. Whenever it
“hears” the keyboard being pressed, it figures out exactly which key is pressed and assigns a key code
to it. The key code is a number (for example, 40 or 37) that corresponds to a specific key on the key-
board. Luckily you don't need to know or remember what these key codes actually are. The Gau^k]n`
class contains convenient properties called HABP, NECDP, QL, and @KSJ that you can use in place of the
actual key code numbers.
Let's have a look how to use this in the program.
You created a method called kjGau@ksj, whose job is to process this event. It looks like this:
bqj_pekjkjGau@ksj$arajp6Gau^k]n`Arajp%6rke`
w
eb$arajp*gau?k`a99Gau^k]n`*HABP%
w
lh]uan*t)9-,7
y
ahoaeb$arajp*gau?k`a99Gau^k]n`*NECDP%
w
lh]uan*t'9-,7
y
ahoaeb$arajp*gau?k`a99Gau^k]n`*@KSJ%
w
lh]uan*u'9-,7
y
 
Search Nedrilad ::




Custom Search