Formatting ActionScript syntax
769
Place braces around each statement when it is part of a control structure (
if..else
or
for
),
even if it contains only a single statement. The following example shows code that is
written poorly:
// bad
if (numUsers == 0)
trace("no users found.");
Although this code validates, it is poorly written because it lacks braces around the statements.
In this case, if you add another statement after the
trace
statement, the code executes
regardless of whether the
numUsers
variable equals 0:
// bad
var numUsers:Number = 5;
if (numUsers == 0)
trace("no users found.");
trace("I will execute");
Executing the code despite the
numUsers
variable can lead to unexpected results. For this
reason, add braces, as shown in the following example:
var numUsers:Number = 0;
if (numUsers == 0) {
trace("no users found");
}
When you write a condition, don’t
add the redundant
==true
in your code, as follows:
if (something == true) {
//statements
}
If you are compare against
false
, you could use
if (something==false)
or
if(!something)
.
Writing a for statement
You can write the
for
statement using the following format:
for (init; condition; update) {
// statements
}
The following structure demonstrates the
for
statement:
var i:Number;
for (var i = 0; i<4; i++) {
myClip.duplicateMovieClip("newClip" + i + "Clip", i + 10, {_x:i*100,
_y:0});
}
Remember to include a space following each expression in a
for
statement.
Summary of Contents for FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Page 1: ...Learning ActionScript 2 0 in Flash...
Page 8: ...8 Contents...
Page 18: ...18 Introduction...
Page 30: ...30 What s New in Flash 8 ActionScript...
Page 66: ...66 Writing and Editing ActionScript 2 0...
Page 328: ...328 Interfaces...
Page 350: ...350 Handling Events...
Page 590: ...590 Creating Interaction with ActionScript...
Page 710: ...710 Understanding Security...
Page 730: ...730 Debugging Applications...
Page 780: ...780 Deprecated Flash 4 operators...
Page 830: ...830 Index...