I l @ ve RuBoard |
Parts of a ScriptWhen 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.
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 |