PDA

View Full Version : Why Doesn't This Work... Targetting An MC Inside MC



tomyknoker
04-22-2007, 12:53 PM
Inside my mc btn1 I have another mc called 'arrows' but when I target it nothing happens, it's meant to fade _alpha to 0... Any ideas?


for (i=0; i<numOfBtn+1; i++) {
this["btn"+i].num = i;
this["btn"+i].onRollOver = function() {
this.play();
this.arrows.alphaTo("0", .5, "easeOutQuad", 0);
};

nwalton
04-23-2007, 04:10 PM
I've found that using the "this" keyword too much can get you into trouble. I'm guessing that it's not referencing the right mc when you're using it inside your function. You can find out by adding this line to your onRollOver function (you probably already know how to do this):


trace(this);

I would actually rewrite your loop to reference the correct movieclip only once, and then you know you've always got the right one:


for (i=0; i<numOfBtn+1; i++) {
var my_mc:Movieclip = this["btn"+i];
my_mc.num = i;
my_mc.onRollOver = function() {
my_mc.play();
my_mc.arrows.alphaTo("0", .5, "easeOutQuad", 0);
}
}

If that doesn't work, I'd run some more trace actions to see what you're actually referencing. You might also check the syntax of your "alphaTo" function, etc.

jamiller
04-27-2007, 08:06 PM
Looks like it's a problem with the for loop. I'm not even sure why you have the my_mc.num = i in there.

This is how I would write it:


for(var i = 0; i<=numOfBtn; i++) {
var my_mc = this["btn"+i];
my_mc.onRollOver = function() {
this.play();
this.arrows.alphaTo("0", .5, "easeOutQuad", 0);
}
}


It also might just be a referencing problem because you reference my_mc inside my_mc when it should simply be "this." Hope this helps.

nwalton
04-27-2007, 08:21 PM
Since the variable my_mc is defined in the same space as the onRollover function, references within the function will refer back to it. It could just be "this", but like I said I try to avoid using the "this" keyword unless I have to. It seems to usually just cause headaches.

I only included the my_mc.num = i code because it was in the original code by tomyknoker. It may be a variable that's referenced somewhere else in the movie.

I never heard whether my code fixed the problem or not though.