Flash MX/ActionScript Graphics tutorial (vi)

Source: Internet
Author: User
Tags numeric
Tutorial

  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

function number () can be used to extract the numbers displayed in the text object on the interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.