Flash as learning: For Loop and exit loop

Source: Internet
Author: User
Tags array exit execution
Cycle

Demo Effect:

Click here to download the source file

Using for is an important means of optimizing as. If we want to 20 MC implementation in response to mouse click events, if not for the direct write then need dozens of or hundreds of lines, and for only a few lines to use. See we achieve this effect: on the stage 20 MC Clicks, in the MC in the dynamic text display the MC's instance name.
Step: 1, make a MC,MC contains a dynamic text, the instance name is txt.

2, hold down CTRL, drag and copy out 20 MC.

3, for each MC input instance name mc0, MC1, Mc2...mc19, then select the frame, open the Action panel, enter the following statement.

mc0.onpress = function () {
This.txt.text = This._name;
};
mc1.onpress = function () {
This.txt.text = This._name;
};
.....
mc19.onpress = function () {
This.txt.text = This._name;
};

The above statement a total of 60 lines, to achieve the click of MC, in the MC dynamic text TXT display the MC instance name, if there are 100 MC, then according to this will need 300 lines, very scary. Now, for a few lines.

for (Var i=0;i<20;i++) {
This["MC" +i].onpress=function () {
This.txt.text=this._name
}
}

Let's analyze how this for solves the problem: for (var i=0;i<20;i++) read 20 times, first i=0, check i is less than 20, check to meet the criteria, execute i++, then I=1, and then execute the statement in {}; second time i= 1; Check if I is less than 20, check to meet the conditions, execute i++, then i=2, and then execute the statements in {}, the third i=2, check i is less than 20, check to meet the conditions, execute the i++, then i=3, and then execute the statements in {}; The 21st time i=20 check i is less than 20, check to do not meet the conditions, the implementation of i++, so i=21, at this time the end of the loop, exit the loop .
The first procedure that is executed is

This["MC" +0].onpress=function () {//This.mc0.onpress=function ()
This.txt.text=this._name
}

The second execution of the procedure is

This["MC" +1].onpress=function () {//This.mc1.onpress=function ()
This.txt.text=this._name
}

The third execution procedure is

This["MC" +2].onpress=function () {//This.mc2.onpress=function ()
This.txt.text=this._name
}

......
From this, we can see that for the simple statement has a very important role, for the application of the MC for the instance name is a continuous case, that is, mc0, MC1, MC2 ... if the instance name of the MC is not contiguous, then the array can be used to solve the problem. First, the instance of the MC is the element in the array, and then called, as follows:

var myarr=["mc0", "MC1", "MC2", "MC3" ...
for (Var i=0;i<20;i++) {
This[myarr[i]].onpress=function () {
This.txt.text=this._name
}
}

The above describes the use of for, now about how to exit the For loop, if we only read 10 MC, then

for (var i = 0; i<20; i++) {
if (i>=10) {
Break
}
This["MC" +i].onpress = function () {
This.txt.text = This._name;
};
}

If we only don't read the 10th one, then

for (var i = 0; i<20; i++) {
if (i = = 10) {
Continue
}
This["MC" +i].onpress = function () {
This.txt.text = This._name;
};
}

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.