PDA

View Full Version : Dynamic MovieClip Names



xohol
January 14th, 2008, 03:18 PM
Ok, im just making the switch over to AS3.

ive tried to search, but can't find the results (im not even sure what this is called)

how do i convert the following into AS3? Specifically the main["b" + i]. My buttons are called b1, b2, b3 etc, and i could loop an action to them in AS2 by using the following loop. Doesn't seem to work now.
thanks!


var main:MovieClip = this
var i:int;
for (i=1; i<=8; i++)
{
main["b" + i].addEventListener (MouseEvent.MOUSE_DOWN, bRelease);
}

dail
January 14th, 2008, 04:01 PM
Try,

var b:MovieClip;
var i:int;
for (i=1; i<=3; i++) {
[b+i];
addEventListener(MouseEvent.MOUSE_DOWN, bRelease);
}

function bRelease(event:MouseEvent):void {
trace("clicked "+event.target.name);
}

xohol
January 14th, 2008, 04:08 PM
thanks, it did work.
can you please explain what [b+i]; is, and why it worked.

now what if i want to pass variables to those buttons?

xohol
January 14th, 2008, 04:12 PM
ok, that code added the bRelease function to every MovieClip on the stage.

how to i only add it to buttons b1 - b8?

xohol
January 14th, 2008, 04:21 PM
i got it to work. i dont know what i did different, if anything, but now it works


var i:int;
for (i=1; i<=8; i++)
{
main["b" + i].addEventListener (MouseEvent.MOUSE_UP, bRelease);
main["b" + i].datahold = folders[i-1]
}

dail
January 14th, 2008, 05:08 PM
Oops, I didn't test that out all that well. Right you are, it does add that action to all instances on the stage..:cantlook:

Your solution works fine now however. The b was just a reference to a movieClip, I was being lazy and wrote "b" instead of "main"...