|
Flash uses a built-in programming language called ActionScript which allows you to control aspects of your Flash movie. You can provide flexibility and control over your movie as ActionScript does all the heavy lifting behind the scenes. The preloader is a good example of this because it completes a test condition -- if frameisloaded() etc, without the viewer knowing all the details. ActionScript allows you to declare variables using the var keyword but not their type. For example, you can declare an ActionScript variable you call total this way: var total = 0; Unlike Java which is a strongly typed language, ActionScript (like JavaScript) is weakly typed. This means there aren't any rules enforced over the use of the variables. They can be treated as numbers and strings almost interchangeably. ActionScript code goes into any frame of your movie, keyframe or regular
frame. You will not see ActionScript code appear in the movie (unless you
type it in on the stage-but then Flash will just ignore it if you do). Any
frame containing ActionScript will have an "a" in the frame in the
timeline. On the right side of the Frame Actions window is the area where you can enter ActionScript code. In the sample ActionScript below note how the statements are terminated with a semicolon and scoping is indicated by the use of the { and }. The ActionScript keywords are indicated in bold. These are var (for declaring variables), true,false (for setting a variable to a boolean), while (for constructing a while loop), length (this is applied to a string variable to obtain its length), charAt(i) (this returns the character of the string at position i), break (this causes the loop to end at that point), trace (causes an Output window to appear). Sample ActionScript var source = "The quick brown fox jumped over the lazy dog"; This code will loop through each letter in the string variable called source and see if any letters match the target letter. If the code finds a match, the loop ends (the break statement) at that point. The trace function causes a window titled Output to appear. Only when you are testing the movie will this window appear - it won't if you publish and then run the Flash movie. The Output window will display "Found (18)x in The quick brown fox jumped over the lazy dog." The notation (found?"Found":"Didn\'t find") is an advanced shortcut of the if-then-else clause. The variable found is either true or false. If it is true, then the true part is applied and that is indicated by the ? symbol. If found is false, then the false part is applied and that is indicated by the : symbol. Note how the ActionScript's trace statement used the "+" operator
to construct a new string by sticking variables together (this process is called
concatenation). The variables don't have to be all strings as you can see
because "i" is used as an integer. |