About statements
145
Using the if..else statement
The
if..else
conditional statement lets you test a condition and then execute a block of
code if that condition exists or execute an alternative block of code if the condition does
not exist.
For example, the following code tests whether the value of
x
exceeds 20, generates a
trace()
statement if it does, or generates a different
trace()
statement if it does not:
if (x > 20) {
trace("x is > 20");
} else {
trace("x is <= 20");
}
If you do not want to execute an alternative block of code, you can use the
if
statement
without the
else
statement.
The
if..else
statement in Flash is similar to the
if
statement. For example, if you use the
if
statement to validate that a user’s supplied user name and password matches a value stored
in a database, then you might want to redirect the user based on whether the user name and
password are correct. If the login is valid, you can redirect the user to a welcome page using
the
if
block. However, if the login was invalid, you can redirect the user to the login form
and display an error message using the
else
block.
To use an if..else statement in a document:
1.
Select File > New and then select Flash Document to create a new FLA file.
2.
Select Frame 1 of the Timeline, and then type the following ActionScript in the
Actions panel:
// create a string that holds AM/PM based on the time of day.
var amPm:String;
// no parameters pass to Date, so returns current date/time.
var current_date:Date = new Date();
// if current hour is greater than/equal to 12, sets amPm string to "PM".
if (current_date.getHours() >= 12) {
amPm = "PM";
} else {
amPm = "AM";
}
trace(amPm);
3.
Select Control > Test Movie to test the ActionScript.
In this code, you create a string that holds
AM
or
PM
based on the current time of day. If the
current hour is greater than or equal to 12, the
amPM
string sets to
PM
. Finally, you trace the
amPm
string, and if the hour is greater than or equal to 12,
PM
is displayed. Otherwise,
you’ll see
AM
in the Output panel.
Содержание FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Страница 1: ...Learning ActionScript 2 0 in Flash...
Страница 8: ...8 Contents...
Страница 18: ...18 Introduction...
Страница 30: ...30 What s New in Flash 8 ActionScript...
Страница 66: ...66 Writing and Editing ActionScript 2 0...
Страница 328: ...328 Interfaces...
Страница 350: ...350 Handling Events...
Страница 590: ...590 Creating Interaction with ActionScript...
Страница 710: ...710 Understanding Security...
Страница 730: ...730 Debugging Applications...
Страница 780: ...780 Deprecated Flash 4 operators...
Страница 830: ...830 Index...