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
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
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
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
}
}
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.