Programming | Tutorials
1.4.4 Code Writing
Now it's time to get into the most troublesome steps-code writing-and we'll be exposed to a lot of new things.
1. text File Data loading
At the very beginning, we created a text file named "Subtrahend.txt," which contains the parameters that the program needs to read in the directory where the FLA file resides, and now we're going to write code to read the data.
Select the first frame in the script layer, and then open the behavior panel and enter such code:
var externalData:LoadVars = new LoadVars();
externalData.onLoad = function(){
subtrahend_txt.text = externalData.subtrahend;
}
externalData.load("subtrahend.txt");
var externaldata:loadvars = new Loadvars (); The function of this statement is to create a Loadvars object named Externaldata.
The next three-line statement is the code that handles the onload event for the Externaldata object, that is, when the Externaldata object is loaded (OnLoad), The Subtrahend property of the Externaldata object is read and assigned to the Subtranhend_txt on the interface, thus completing the process of reading the data from the text file and displaying it on the interface.
You might ask, "Where does the subtrahend attribute of the Externaldata object come from", and look back at what we entered in the text file at the beginning of the instance is &subtrahend=100, where the line "&" The Loadvars object identifies the word after & as one of its properties and takes the argument to the right of the immediately following sign as the value of the property.
2, the user input data processing
This subtraction operation, two arithmetic one is read from the text file, the other is based on the user's input, the previous set of text object properties we have set to "input text", where the user can enter a value. Now is the action to be taken when the user completes the numeric input and then clicks the Submit button.
Select the Submit button in the script layer to add the code to it:
on (press) {
var minuend:Number = Number(minuend_txt.text);
var subtrahend:Number = Number(subtrahend_txt.text);
finalResult=minuend-subtrahend
}
This code handles what the user needs to do after clicking on the named button, where the two statements convert the contents of the two text objects displayed on the interface to numeric values and save them with two variables minuend and subtrahend respectively. It then operates the difference between the two and saves it to the variable finalresult.
3, according to the results of the operation to provide feedback
First drag and drop an instance of a movie clip movie_clip to the "interface" layer of the scene and name the movie clip instance as MOVIE_CLIP_INSTANCE_MC, and you must specify a name for the instance of the movie clip to refer to in your code.
The following is based on the value of Finalresult to determine what feedback to the user. Append the following code to the on (press) event processing.
on (press) {
var minuend:Number = Number(minuend_txt.text);
var subtrahend:Number = Number(subtrahend_txt.text);
finalResult=minuend-subtrahend;
result_txt.text=finalResult;
if (finalResult> 0)
Movie_Clip_Instance_mc.gotoAndPlay(3)
else
Movie_Clip_Instance_mc.gotoAndPlay(2);
}
This additional line of code uses a simple judgment structure, if...else ..., executes the previous statement if the condition in the if bracket is satisfied, or executes the latter statement. Here we call the gotoAndPlay method of the movie clip instance MOVIE_CLIP_INSTANCE_MC, which allows the movie clip to play inside itself, because we have different feedback on different frames in the movie clip. Therefore, this statement can be very fast to complete the user feedback, which is the interactive Flash application development of the common techniques.
1.5 Summary
Though small, spite, this program is a bit shabby, but through this silly example we can still learn a lot of flash MX and ActionScript knowledge, here a review, check the following list of how much you have mastered:
Flash program input data There are two more common methods: first, through user input, in this case we use an "input text" to achieve, there is a text file, in this example, we create a Loadvars object and call its Load method, and then the OnLoad () Extract data from event processing.
In the On (press) event, you can handle the action that the user clicks the button
Use the gotoAndPlay () method of the movie clip instance to play the frame specified inside the movie clip instance
The function number () can be used to extract the numbers displayed in the text object on the interface.