1.3. Performing Actions Conditionally
Problem
You want to perform an action only when a condition is true.
Solution
Use an if statement or a
switch statement.
Discussion
You often need your ActionScript code to make decisions, such as
whether to execute a particular action or group of actions. To
execute an action under certain circumstances, use one of
ActionScript’s conditional
statements: if, switch, or
the ternary conditional operator (? :).
The conditional statements allow you to make logical decisions, and
you’ll learn from experience which is more
appropriate for a given situation. The if
statement is most appropriate when you want to tell your Flash movie
to do something only when a certain condition is met (i.e., when the
condition is true). When you have several possible conditions to
test, you can use the switch statement instead.
And you can use Flash’s ternary conditional operator
to perform conditional checking and assignment on a single line.
First, we’ll look at the if
statement. Of the conditional statements in ActionScript, the
if statement is the most important to
understand. In its most basic form, an if
statement includes the keyword if followed by the
test expression whose truthfulness you want to evaluate to determine
which action or actions to execute. The test expression must be in
parentheses, and the statement(s) to be executed should be within
curly braces (the latter is mandatory if there is more than one
statement in the statement block).
Here, we check ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access