Game Development Reference
In-Depth Information
Moving incrementally
The buttons work: they move the cat up and down the hill. But wouldn't it be nice if you could move
the cat down the hill gradually? That would be a much more realistic effect and make the cat toy
a little more fun to play with. Fortunately, this is very easy to do:
1. Update the directives in the kjQl>qppkj?he_g and kj@ksj>qppkj?he_g event handlers with the
following new text in bold:
bqj_pekjkjQl>qppkj?he_g$arajp6IkqoaArajp%6rke`
w
dehhL]ca*_]p*u9dehhL]ca*_]p*u)-17
y
bqj_pekjkj@ksj>qppkj?he_g$arajp6IkqoaArajp%6rke`
w
dehhL]ca*_]p*u9dehhL]ca*_]p*u'-17
y
2. Save the I]ej*]o file and test the project.
Now each time you click the up or down button, the cat moves 15 pixels in either direction. Much
better!
But how did this work? The logic behind it is very simple once you get your head around it. Let's have
a look at the kj@ksj>qppkj?he_g directive:
dehhL]ca*_]p*u9dehhL]ca*_]p*u'-17
This directive takes the current y position of the cat, adds 15 pixels to it, and then reassigns the new
total back to the cat's current y position. Think of it this way: the cat's new position is a combination
of its position before the button was clicked, plus 15 pixels. You want to move the cat down the stage,
so you need to increase its y position.
I know, this is a bit of a brain-twister! Let's break it down a little more. The starting position of the cat
in my program is 120 pixels. Whenever the program sees dehhL]ca*_]p*u, it interprets that to mean
“120 pixels.” You've set up the program so that every time the down button is clicked, 15 pixels are
added to the cat's y position. That's what this part of the directive in bold does:
dehhL]ca*_]p*u9dehhL]ca* _]p*u'-1 7
It just adds 15 to the cat's y position, so the cat's new y position is 135. So you could actually write the
directive this way:
dehhL]ca*_]p*u9-/17
Pretty simple, really, isn't it?
 
Search Nedrilad ::




Custom Search