Game Development Reference
In-Depth Information
lh]uanD]o=llha9pnqa7
y
ahoa
w
op]ca*]``?deh`$]llha%7
]llha*t9lh]uan*t7
]llha*u9lh]uan*u7
lh]uanD]o=llha9b]hoa7
eb$ajaiu*depPaopK^fa_p$]llha%%
w
iaoo]ca@eolh]u*patp9Pd]jgo7
y
y
y
To understand how this works, think about what conditions need to be met before the lh]uan object
can either pick up or drop the apple:
The game player needs to press the spacebar. You can check for this condition with a condi-
tional statement that looks like this: $arajp*gau?k`a99Gau^k]n`*OL=?A%.
The lh]uan object needs to be touching the apple. You can check for this condition using
a conditional statement that looks like this: $lh]uan*depPaopK^fa_p$]llha%%.
If those two things happen at the same time, you know that the player is either trying to pick up or
drop the apple. But the key is that both conditions need to be true at exactly the same time . Why? Well,
obviously it wouldn't make sense if the lh]uan object could pick up the apple if it weren't touching it,
and the game also needs to know that the player wants to pick up the object. In this example, you tell
the game that you want to pick up or drop the apple by pressing the spacebar.
To check whether two conditions are pnqa at the same time, you can combine them into a single con-
ditional statement using the ]j` operator. You looked at the ]j` operator briefly in Chapter 4, but you
haven't really seen it in action until now. The ]j` operator is a double ampersand that looks like this:
""
It's used in the first new eb statement in this line of code:
eb$arajp*gau?k`a99Gau^k]n`*OL=?A""lh]uan*depPaopK^fa_p$]llha%%w
Great! In one line of code, you can check whether both the conditions for picking up or dropping
objects have been met. But which one is it? Picking up or dropping?
That's pretty easy to figure out. If those two conditions are pnqa, and the player doesn't already have
the apple, you know that the apple needs to be picked up. If the player already has the apple, you
know the apple should be dropped. All it requires is an additional nested eb/ahoa statement that
checks whether the lh]uanD]o=llha variable is b]hoa. Here's a simplified version of this logic:
eb$lh]uanD]o=llha%
w
Le_gpda]llhaql***
y
 
Search Nedrilad ::




Custom Search