Parts of a Script

I l @ ve RuBoard

Parts of a Script

When you write your own scripts, you will use all sorts of different keywords and characters . To learn these, it will be useful to look at a real script and examine its parts.

We'll use the following script as an example. It is a button script that runs when the user clicks the button that the script is attached to. It doesn't perform any particular function, but rather demonstrates several major ActionScript constructs.

 on (press) {     var myVariable = 7;     var myOtherVariable = "Macromedia";     for (var i=0; i<10; i++) {         trace(i);         if (myVariable + 3 == 5) {             trace(myOtherVariable);         }     } } 

The first line of the script identifies the rest as something that executes when the user first presses the button. The on (press) construct can be used only in button scripts. You can also use on (release) if you want the code to execute when the user completes the button press.

The curly bracket , { , at the end of the first line signifies that this is the beginning of a code segment. From the open bracket, { , to its corresponding closing bracket, } , all the code inside the brackets is part of a set of code that belongs together. In this case, the code segment represents all the code that is executed when the button is pressed.


Notice that the indentations of the code example follow the brackets. The line after an open bracket is indented one tab stop farther, and every line that follows it is at the same level until the corresponding close bracket, or another open bracket takes the code one tab stop farther. This type of indentation makes it much easier to read the code and is common among computer languages. In fact, Flash will indent your code for you by default.

The first line of this code segment creates a new local variable called myVariable . It assigns the value of 7 to it. The new line assigns the string "Macromedia" to the variable myOtherVariable . We'll look more at Flash variables in the section "Local and Global Variables " later this hour .

The semicolon at this end of this line, and many other lines, signifies the end of the instruction. Put a semicolon after every line that is a complete instruction.

The for structure starts a loop. In this case, it loops 10 times, with the variable i starting at 0 and increasing to 9. We'll look at loops in the section "Loops," later this hour. The bracket at the end of the for line signifies the start of the code segment that will be the body of the loop.

The trace command sends its contents to the Output window. We'll see more of that command in the upcoming section "The Output Window."

The if command is called a conditional statement. It tests the statement that follows it, myVariable + 3 == 5 , to see whether it is true. If so, the code segment in the brackets is executed. If not, this whole code segment is skipped .

The code segment in question is a single trace command that sends the variable myOtherVariable to the Output window.

The rest of the example consists of three close brackets. The first closes the if statement. The second closes the for loop, and the third bracket closes the on (press) segment.

Now that you have seen a complete, though small, ActionScript program, let's take a closer look at some specific parts of ActionScript.

I l @ ve RuBoard

Sams Teach Yourself Flash MX ActionScript in 24 Hours
Sams Teach Yourself Flash MX ActionScript in 24 Hours
ISBN: 0672323850
EAN: 2147483647
Year: 2002
Pages: 272 © 2008-2017.
If you may any questions please contact us: