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.