Game Development Reference
In-Depth Information
Try clicking the up and down buttons. The cat is now prevented from moving beyond a certain dis-
tance. Exactly the effect you want to achieve!
Let's look at how the eb statement works in the kjQl>qppkj?he_g event handler:
eb\$dehhL]ca*_]p*u:9-.,%
w
dehhL]ca*_]p*u)9-17
y
The key to making it work is the conditional statement inside the parentheses:
eb \$dehhL]ca*_]p*u:9-.,%
Conditional statements are used to check whether a certain condition is pnqa or b]hoa. The preceding
conditional statement is checking to see whether the y position of the cat is greater than or equal to
120. If the condition resolves as pnqa, the directive it contains inside the curly braces is executed.
Conditional statements use conditional operators to do their checking for them. The conditional
operator used in the eb statement is the greater-than-or-equal-to operator. It looks like this:
:9
This operator checks whether the value on its left (the cat's y position) is greater than or equal to the
value on its right. There are many conditional operators available to use with AS3.0, and Table 4-3
shows the most common ones.
Table 4-3. Condition al Operators
Symbol
Name
What it does
==
Equality operator.
Literally means “is equal to.” Checks to see whether
two values are equal to one another.
10 == 10 returns a value of pnqa.
10 == 3 returns a value of b]hoa.
Make sure that you don't confuse the equality operator
(==) with the assignment operator (=). This is a com-
mon confusion! Remember that = means “gets the
value of” and is used to assign values to variables or
properties. == is used to compare values.
There is a strict version of the equality operator that
makes sure that the two values are of the same type
before the comparison is made. This can help avoid
certain kinds or errors that happen when trying to
compare incompatible value types, such as trying to
compare apples and oranges. The strict equality
operator looks like this:
===
You should know that this strict version exists, but you
won't be using it in this topic.