Statement Syntax

As you've seen, statements are keywords, operators, and identifiers joined together to accomplish certain tasks. For instance, in the following code, var and new are the keywords, Array () is the identifier, :Array is the data type, and the equal sign is the operator:

 var my_array:Array = new Array(); 

As you'll notice, a semicolon has been placed at the end of this statement. This semicolon tells the interpreter that the statement is complete and to move on to the next one. The semicolon is not required, and the interpreter will move on without it. However, it is good coding etiquette to place one there.

Also, it is good etiquette to place each new statement on its own line. Again, this is not necessary, but it is a good practice to follow. You can see this for yourself by examining the following two segments of code. Which code section is easier to read?

 var myVariable:String = "Flash"; myVariable += " Unleashed"; trace (myVariable); //output: Flash Unleashed myVariable = "Flash"; myVariable += " Unleashed"; trace (myVariable); //output: Flash Unleashed 

Although the output is the same, the first section of code is much easier to read than the second. Note the spacing between each part of the statement. Often, this is a necessity for the interpreter to correctly identify each part. However, even if this spacing is not always required, it is always a good rule to follow.

Statement Block

Some statements have multiple statements associated with them, particularly flow modifiers. These statements have statements within them that appear between brackets. Let's take a look at an example:

 if (book == "Flash Unleashed") {     trace ("Your on the right track"); } 

The first statement is an if statement (if statements are discussed in greater detail later in this chapter) that contains a function statement. Notice that not only is the trace function held between brackets, but it is indented as well. This indentation is not a requirement but is used for improved readability. You can, however, turn on the option to have statements indent automatically: Choose Auto Format under the ActionScript preferences or press Ctrl+Shift+F. You can even adjust the settings of the automatic formatting under Auto Format preferences.

Also, note that the lines with curly brackets do not have semicolons. You can put semicolons on closing curly brackets, but it is not necessary. What's more, the closing bracket is aligned with the beginning of the line that the opening bracket is on. Again, this is not a requirement; it's just placed this way for ease of readability.

The closing bracket is required if an opening bracket is used; otherwise, the interpreter will send an error message like this one:

 Statement block must be terminated by '}' 

Even though the earlier code is in brackets, because only one statement is held within the if statement, the use of brackets is not required. Instead, the code can be written like this:

 if (book == "Flash Unleashed") trace ("You're on the right track"); 

As a personal preference, I use brackets in conditional statements, even if they are not required, just for consistency.

Another type of statement that uses brackets is a user-defined function. Here's an example:

 function myFunction (myVariable:String):Void{       trace (myVariable); } var name:String = "David"; myFunction (name); //output: David 

Again, the statement held within the function appears between brackets and is also indented for easy reading and consistency.

Now that we have gone over some of the basic syntax of statements, let's cover some of the statement categories in more detail.

Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319 © 2008-2017.
If you may any questions please contact us: