Highlighted

For Loop - Increment 'i' for each MovieClip

Explorer ,
Jul 30, 2019

Copy link to clipboard

Copied

Hello,

I am trying to have a for loop which checks 5 MovieClips to see if they are within range of my target object. In order to do this more efficiently I want to use a for loop to check each 5 at once (hit1_mc, hit2_mc etc)

For example:

for (i = 0; i < 5; i++) {

     if(hit+i+_mc.x == 500){ // I wish to increment the movieclip by 1 each time, checking whether the x co-ordinate is equal to 500

          //Do something

     }

}

I believe i may need to convert the movieclip name to a string first? However I am struggling to work it out!

Any help would be great!

Thanks

Adobe Community Professional
Correct answer by JoãoCésar | Adobe Community Professional

Hi.

Use bracket notation.

Like this:

AS3:

import flash.display.MovieClip;

for (var i:int = 0; i < 5; i++)

{

    var mc:MovieClip = this["hit" + (i + 1) + "_mc"];

  

    if (mc.x == 500)

        trace(mc.name);

}

JavaScript:

for (var i = 0; i < 5; i++)

{

    var mc = this["hit" + (i + 1) + "_mc"];

  

    if (mc.x === 500)

        console.log(mc.name);

}

Regards,

JC

TOPICS
Discussions

Views

97

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

For Loop - Increment 'i' for each MovieClip

Explorer ,
Jul 30, 2019

Copy link to clipboard

Copied

Hello,

I am trying to have a for loop which checks 5 MovieClips to see if they are within range of my target object. In order to do this more efficiently I want to use a for loop to check each 5 at once (hit1_mc, hit2_mc etc)

For example:

for (i = 0; i < 5; i++) {

     if(hit+i+_mc.x == 500){ // I wish to increment the movieclip by 1 each time, checking whether the x co-ordinate is equal to 500

          //Do something

     }

}

I believe i may need to convert the movieclip name to a string first? However I am struggling to work it out!

Any help would be great!

Thanks

Adobe Community Professional
Correct answer by JoãoCésar | Adobe Community Professional

Hi.

Use bracket notation.

Like this:

AS3:

import flash.display.MovieClip;

for (var i:int = 0; i < 5; i++)

{

    var mc:MovieClip = this["hit" + (i + 1) + "_mc"];

  

    if (mc.x == 500)

        trace(mc.name);

}

JavaScript:

for (var i = 0; i < 5; i++)

{

    var mc = this["hit" + (i + 1) + "_mc"];

  

    if (mc.x === 500)

        console.log(mc.name);

}

Regards,

JC

TOPICS
Discussions

Views

98

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jul 30, 2019 0
Adobe Community Professional ,
Jul 30, 2019

Copy link to clipboard

Copied

Hi.

Use bracket notation.

Like this:

AS3:

import flash.display.MovieClip;

for (var i:int = 0; i < 5; i++)

{

    var mc:MovieClip = this["hit" + (i + 1) + "_mc"];

  

    if (mc.x == 500)

        trace(mc.name);

}

JavaScript:

for (var i = 0; i < 5; i++)

{

    var mc = this["hit" + (i + 1) + "_mc"];

  

    if (mc.x === 500)

        console.log(mc.name);

}

Regards,

JC

__________________________________________
HTML5, JSFL, and AS3 samples: http://bit.ly/2mJgDoG

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 30, 2019 2
Adobe Community Professional ,
Jul 30, 2019

Copy link to clipboard

Copied

use bracket notation to force animate to coerce a string to an object:

for(i=0;i<5;i++){

if(this["hit"+(i+1)+"_mc"].x==500){

// do something

}

}

but generally it's a bad idea to check for exact equality of position properties (among others).  you probably want to use:

for(i=0;i<5;i++){

if(Math.abs(this["hit"+(i+1)+"_mc"].x-500)<=.05){

// do something

}

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 30, 2019 1
ClayUUID LATEST
Adobe Community Professional ,
Jul 30, 2019

Copy link to clipboard

Copied

kglad  wrote

use bracket notation to force animate to coerce a string to an object:

Bracket notation does not perform coercion in the sense that that word is normally used. It's an alternate syntax for accessing object properties.

And, if any coercion were happening, it would be the browser's JavaScript interpreter doing it, not Animate.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 30, 2019 0