• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

For Loop - Increment 'i' for each MovieClip

Explorer ,
Jul 30, 2019 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

Views

219

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
community guidelines

correct answers 1 Correct answer

Community Expert , Jul 30, 2019 Jul 30, 2019

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

Votes

Translate

Translate
Community Expert ,
Jul 30, 2019 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

Votes

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
community guidelines
Community Expert ,
Jul 30, 2019 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

}

}

Votes

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
community guidelines
LEGEND ,
Jul 30, 2019 Jul 30, 2019

Copy link to clipboard

Copied

LATEST

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.

Votes

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
community guidelines