PDA

View Full Version : For-loop over if-loop over movieclips


roemraw
08-07-2010, 08:59 PM
Hi, I am new to this site. I am not that new at AS, but I am definitly no pro.
I think the best way to describe my problem is to just show the code, so here goes:

var o = MovieClip.prototype;
var n:Number = 6;
var shift:Number = 0;
o.onRelease = function() {
for (i=1; i<(n); i++) {
mc = "_level0.MC"+i;
trace(mc)
trace(this)
if (this == mc) {
trace("hi")
shift = i;

}
}
}

Then when I click on a movieclip I get the following from the trace commands:

_level0.MC1
_level0.MC2
_level0.MC2
_level0.MC2
_level0.MC3
_level0.MC2
_level0.MC4
_level0.MC2
_level0.MC5
_level0.MC2

This shows that it correctly performs the trace commands in the fo-loop, the trace(mc) and trace(this).
As you can see
this == _level0.MC2
and the mc is being looped over and at one point it is also equal to "_level0.MC2"

My question is, why does the if-statement not recognize this correctly and respond by tracing:"hi" ?


The basic I dea is I have movieclips named MCi for i = 1 to n.
And I want to be able to track on which one I clicked by it returning me the i from their name.
So to say if I click on MC4 I want it to return the number 4 to me and when I click on MC98 I want it to return 98 to me.

Thanks in advance!

tango88
08-08-2010, 08:46 AM
If I'm not mistaken, mc is a string and this is an object, so mc will never equal 'this', although the string has the same value as the name of the object.

If you phrase it this way, it could work:

mc = this["_level0.MC"+i];

...therefore mc refers to an object with the name "_level0.MC"+i

roemraw
08-08-2010, 12:23 PM
I tried it, but it didn;t work.
But what you said, made me think.
If mc was a string, then I;ll just use the string attached to "this"

so I changed the code to:

mc = "MC"+i;
trace(mc)
trace(this._name)
if (this._name == mc) {
trace("hi")
shift = i
}

And now it does work. So thanks for helping me on the right direction!