MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH Manual page 145

Learning actionscript 2.0 in flash
Table of Contents

Advertisement

Using the if..else statement
The
conditional statement lets you test a condition and then execute a block of
if..else
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
statement if it does, or generates a different
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
without the
statement.
else
The
statement in Flash is similar to the
if..else
statement to validate that a user's supplied user name and password matches a value stored
if
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
block. However, if the login was invalid, you can redirect the user to the login form
if
and display an error message using the
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
current hour is greater than or equal to 12, the
string, and if the hour is greater than or equal to 12,
amPm
you'll see
in the Output panel.
AM
x
statement if it does not:
trace()
statement. For example, if you use the
if
block.
else
or
based on the current time of day. If the
AM
PM
string sets to
amPM
exceeds 20, generates a
statement
if
. Finally, you trace the
PM
is displayed. Otherwise,
PM
About statements
trace()
145

Hide quick links:

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH and is the answer not in the manual?

This manual is also suitable for:

Flash 8

Table of Contents