EasyManua.ls Logo

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH - Page 190

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
830 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
190 Syntax and Language Fundamentals
Using relational and equality operators
Relational and equality operators, also called comparison operators, compare values of
expressions, and they return either
true or false (a Boolean value). You frequently use
comparison operators in conditional statements and loops to specify the condition for when
the loop should stop.
You can use the equality (
==) operator to figure out whether the values or references of two
operands are equal, and this comparison returns a Boolean value. String, number, or Boolean
operand values compare using a value. Object and array operands are compared by a
reference.
In this example, you can see how to use the equality operator to test the arrays length and
display a message in the Output panel if there are no items in the array.
var myArr:Array = new Array();
if (myArr.length == 0) {
trace("the array is empty.");
}
When you select Control > Test Movie, the string the array is empty appears in the
Output panel.
You can use the equality operator to compare values, but you cannot use the equality operator
to set values. You might try to use the assignment operator (=) to check for equality.
To use relational and equality operators in your code:
1. Create a new Flash document.
2. Type the following ActionScript into Frame 1 of the Timeline:
var myNum:Number = 2;
if (myNum == 2) {
// do something
trace("It equals 2");
}
In this ActionScript, you use the equality operator (==) to check for equality. You check
whether the variable
myNum equals 2.
3. Select Control > Test Movie.
The string
It equals 2 appears in the Output panel.
4. Return to the authoring environment and change:
var myNum:Number = 2;
to:
var myNum:Number = 4;

Table of Contents

Related product manuals