Game Development Reference
In-Depth Information
Applying gravity to the object is simply a matter of adding it to the [ru value, which this directive
does:
[ru'9[cn]repu7
That's it! That's all you need to do to implement gravity.
The Lh]uan[Cn]repu class goes a little bit further, however. You want to allow the player to make the
cat move up when the up arrow is pressed. There is actually more than one way to do this, and this
class represents the slightly more complex way.
The way this works is that when the player presses the up arrow key, [cn]repu is set to , in the
kjGau@ksj event handler:
lner]pabqj_pekjkjGau@ksj$arajp6Gau^k]n`Arajp%6rke`
w
eb$arajp*gau?k`a99Gau^k]n`*HABP%
w
[]__ahan]pekjT9),*.7
y
ahoaeb$arajp*gau?k`a99Gau^k]n`*NECDP%
w
[]__ahan]pekjT9,*.7
y
ahoaeb$arajp*gau?k`a99Gau^k]n`*QL%
w
[]__ahan]pekjU9),*.7
[cn]repu9,7
pdeo*a]no*lh]u$%7
y
y
This allows the _]p object to move up the stage freely and also triggers its a]no subobject to start its
animation. The ears are animated using a simple motion tween. Have a look at the A]no symbol in the
Library to take a closer look at how this works. (Notice that there's also no code for the down arrow
key. You don't need it—gravity takes care of moving the object down.)
When the keys are released, the [cn]repu variable is set back to its original value of ,*/.
lner]pabqj_pekjkjGauQl$arajp6Gau^k]n`Arajp%6rke`
w
[]__ahan]pekjT9,7
[]__ahan]pekjU9,7
[cn]repu9,*/7
pdeo*a]no*ckpk=j`Opkl$-%7
y
The ears are also stopped and returned to their starting frame. You can create this same effect by
just giving []__ahan]pekjU a positive value when you need gravity applied. (A positive u value pulls
the object to the bottom of the stage.) It means that you could dispense with having to create the
[cn]repu variable altogether, and the class would contain a bit less code. However, the nice thing
 
Search Nedrilad ::




Custom Search