Conditional constructs
29
Both Lingo and JavaScript syntax provide conventions for altering the default execution order or
script statements, and for performing actions depending on specific conditions. For example, you
may want to do the following in your scripts:
•
Execute a set of statements if a logical condition is true, or execute alternate statements if the
logical condition is false.
•
Evaluate an expression and attempt to match the expression’s value to a specific condition.
•
Execute a set of statements repeatedly until a specific condition is met.
Testing for logical conditions
To execute a statement or set of statements if a specified condition is true or false, you use the
if...then...else
(Lingo) or
if...else
(JavaScript syntax) structures. For example, you can
create an
if...then...else
or
if...then
structure that tests whether text has finished
downloading from the Internet and, if it has, then attempts to format the text. These structures
use the following pattern to test for logical conditions:
•
In both Lingo and JavaScript syntax, statements that check whether a condition is true or false
begin with the term
if.
•
In Lingo, if the condition exists, the statements following the term
then
are executed. In
JavaScript syntax, curly brackets (
{ }
) take the place of the Lingo term
then
, and must
surround each individual
if
,
else
, or
else if
statement.
•
In both Lingo and JavaScript syntax, if the condition does not exist, scripts skip to the next
statement in the handler using the term
else
or
else if
.
•
In Lingo, the term
end if
specifies the end of the
if
test. In JavaScript syntax, the
if
test ends
automatically, so there is no term that explicitly ends the test.
To optimize your script’s performance, test for the most likely conditions first.
The following statements test several conditions. The term
else if
specifies alternative tests to
perform if previous conditions are false:
-- Lingo syntax
if _mouse.mouseMember = member(1) then
_movie.go("Cairo")
else if _mouse.mouseMember = member(2) then
_movie.go("Nairobi")
else
_player.alert("You’re lost.")
end if
// JavaScript syntax
if (_mouse.mouseMember = member(1)) {
_movie.go("Cairo");
}
else if (_mouse.mouseMember = member(2)) {
_movie.go("Nairobi");
}
else {
_player.alert("You’re lost.");
}
Summary of Contents for DIRECTOR MX 2004-DIRECTOR SCRIPTING
Page 1: ...DIRECTOR MX 2004 Director Scripting Reference...
Page 48: ...48 Chapter 2 Director Scripting Essentials...
Page 100: ...100 Chapter 4 Debugging Scripts in Director...
Page 118: ...118 Chapter 5 Director Core Objects...
Page 594: ...594 Chapter 12 Methods...
Page 684: ...684 Chapter 14 Properties See also DVD...
Page 702: ...702 Chapter 14 Properties See also face vertices vertices flat...
Page 856: ...856 Chapter 14 Properties JavaScript syntax sprite 15 member member 3 4...
Page 1102: ...1102 Chapter 14 Properties...