Game Development Reference
In-Depth Information
The first thing that this bit of code did was to figure out if the player object is not on the hill:
eb$ dehh*depPaopLkejp$lh]uan*t(lh]uan*u(pnqa%%
Notice the use of the not () operator. It can help you figure out whether a collision is not occurring ,
which is often just as important as knowing whether one is occurring .
The real magic lies in these two directives:
lh]uan*t)9rt7
lh]uan*u)9ru7
They don't look like much, so what are they doing? It's very simple. They're subtracting the t and u
velocity from lh]uan object's current position. That forces the lh]uan object back to the position it
was before the collision was detected. This effectively blocks the lh]uan object from moving.
The logic behind this is exactly the same as the logic you used to prevent the lh]uan object from
moving beyond the stage boundaries. Remember that if the code detects that the lh]uan object has
moved farther than the edges of the stage, it pushes the object back so it's right at the stage edge.
The previous code does exactly the same thing, with one crucial difference: you know where the
boundaries of the stage are, but you don't know where the boundaries of the dehh object are. However
there are two important things that you do know:
depPaopLkejp (with a jkp operator) can tell you when the player is no longer touching the
dehh object. (This is the equivalent of figuring out whether the player has moved beyond the
edge of the stage.)
All these directives are running within the kjAjpanBn]ia event handler, which means they run
each time the Flash Player ā€œenters a new frame,ā€ 30 times per second. You know that if
depPaopLkejp tells you that the player is no longer touching the dehh object, the player must
have been touching it in the previous frame . This means that all you have to do is to move the
lh]uan object back to where it was in the previous frame to prevent it from moving forward.
You don't know where the player was, but (thanks to the rt and ru variables) you know exactly
how fast it was moving. If you subtract the rt and ru values from the player's current t and u
position, it will be back in the spot it was in before it left the hill. Neat trick, huh?
Let's make a small improvement to this example by allowing the lh]uan
object to move a little farther up the hill before its movement is blocked.
You'll define the bottom-center point of the lh]uan object as the point you'll
use in the collision. Figure 7-32 shows where this point is.
The t position hasn't changed; only the u position has. You can define the
new u coordinate with this simple calculation:
lh]uan*u'lh]uan*daecdp+.
Figure 7-32. Define the bot-
tom center point of the player
object.
 
Search Nedrilad ::




Custom Search