Tutorial
1.3.4 Standalone script file
Open the menu "file-> new" and select "Action Script File" from the New Document dialog box to establish independence. As files, the biggest advantage of this file is that it can be reused. For example, the scripts you create in a project can be placed in a separate. As files, you can call this directly if other items use similar functionality. The code in the as file. This can greatly improve the development efficiency and reduce the degree of redundancy of the code.
1.4 The first scripting program
The following is a very simple ActionScript program to demonstrate the operation of ActionScript, although this example looks a bit "silly", but though small spite, it covers the ActionScript knowledge is more comprehensive. This example
1.4.1 External Data files
Create a plain text file, name it substrahend.txt, enter the following, and then store it in the same folder as the sample scenario.
In the following example, we will read the contents of this file and apply the data in the program, this is an important method of flash application development, that is, "data, Code Separation",. fla file contains as much as possible the execution code of the program, The data to be used in the execution of the program is saved in a separate text file. In this way, if we need to modify the data, we can edit the text file directly without having to go through the hassle of modifying the program. Modifying a program is not only troublesome, easy to neglect, but also easy to make a mess of the program. This is the first time in this series of tutorials to use the "data, Code Separation" method, will be used more in the future, you must master the main points of this method, and then use it, so that your program will be more stable and flexible.
1.4.2 Movie Clips
1. Launch Flash MX 2004, create a new scene file, add a movie clip to it, name Movie_clip, and enter the editing status of this movie clip.
2. Add three frames to this movie clip (the number can be set according to your actual situation), add a text object to each frame, and set the content appropriately. In the later production, we will be in the video clips within the frame of the movie clip to play to achieve feedback on user input. In this example, we set the contents of the three text objects to be "input parameters", "parameter Errors", and "successful operation" respectively.
3. Select these three frames, and enter the statements in the script panel below
This.stop ()
The function of this statement is very simple, is to suspend the movie playback. In the following production, we will put this movie clip into the main scene to create a movie clip instance, and use this example to provide feedback to the user, we certainly do not want users have not input, the film clips are constantly beating, that also talk about what interactivity? So, use the STOP () statement to let the movie clip stop immediately after each frame is played.
There is a this in this statement, and the variable is all that is ever going to be, use this inside a movie clip, this is the movie clip itself, used in a movie clip instance, and he refers to the current movie clip instance itself. So this is a context-dependent variable, and use it with extra care to figure out what kind of context you're using this variable in.
1.4.3 Main Scene Design
1, into the main scene of the editing state, the establishment of two layers, respectively named "Interface" and "Feedback", the interface layer is used to add user input Data window, confirm the input button. The feedback layer adds an instance of the movie clip Movie_clip to feedback the user input.
2, in the "interface" layer to add several text objects, set its contents, except for the second text object set to "input text" (We will use this text object to receive the user's input), the rest of the use of the default "static text."
There are three text objects associated with the following program code, the first text object displays the data read from the text file, its name is minusend_txt (note the named suffix, the principle is explained previously); the second receives the data entered by the user, whose name is Subtrahend_ TXT; the last display data, the name is Result_txt.
3) Now add a third layer and name it "script", click the Add button at the bottom right of the library panel to create a new "button" type of original, simply set the appearance of the button (the focus of our attention in this tutorial is scripting, not the interface), and drag it to the "script" layer.