Game Development Reference
In-Depth Information
In plain English, this means the following:
If the player's t position is more than the stage's maximum width (which is 550), move the player back
to an t position of exactly 550.
The rest of the eb+ahoa statement does exactly the same thing by checking the remaining three stage
edges and then repositioning the lh]uan object if it needs to. It's a trap that the lh]uan object can't
escape from! A pen for the poor little piggy, alas!
Building a better pigpen
So the code works, but can you make it work a little better? It actually looks rather awkward when half
of the lh]uan object manages to squeeze past the edge before being stopped. Wouldn't it be better
if you could stop the whole thing?
To do this, you need a little bit more information about the lh]uan object. You need to know the
following:
What half its width is
What half its height is
After you know this data, you can add half of the lh]uan object's width to its t position and half
its height to its u position. You can then test these new values against the stage's dimensions, which
should stop the lh]uan object before it even manages to sneak as much as a trotter over the edge of
the stage.
You might be catching on to the fact that whenever I talk about ā€œgetting more information,ā€ it usually
means that more variables are needed to store that information. In the next new section of code, you'll
create two new variables, lh]uanD]hbSe`pd and lh]uanD]hbDaecdp, directly inside the kjAjpanBn]ia event
handler. This is a way of creating variables you haven't looked at before. When variables are declared
directly inside a function definition, they're called local variables . More about that in a moment, but first
let's fix up this code!
1. Make the changes and additions to the kjAjpanBn]ia event handler shown in bold text (the
two eb+ahoa statements are considerably changed since the first attempt, so you might want to
rewrite them from scratch instead of trying to update them):
bqj_pekjkjAjpanBn]ia$arajp6Arajp%6rke`
w
++Ejepe]hevahk_]hr]ne]^hao
r]nlh]uanD]hbSe`pd6qejp9lh]uan*se`pd+.7
r]nlh]uanD]hbDaecdp6qejp9lh]uan*daecdp+.7
++Ikrapdalh]uan
lh]uan*t'9rt7
lh]uan*u'9ru7
++Opkllh]uan]pop]caa`cao
eb$lh]uan*t'lh]uanD]hbSe`pd:op]ca*op]caSe`pd%
w
 
Search Nedrilad ::




Custom Search