Copy link to clipboard
Copied
Hi, I have 9 buttons on the main stage and each has an incremented instance name P0001, P0002, P0003...
I am trying to dynamically create and addEventListener on mouse click for each one then call the appropiate function when the button is clicked, this is what i have which doesnt work, am i going about this the wrong way? Thanks
for(var i:int = 1; i<10; i++) {
var btn1:String="P000"+i.toString();
var btn11:Object = btn1;
var ClickBtn:String="Func"+i.toString();
btn11.addEventListener(MouseEvent.CLICK, ClickBtn);
i=i+1;
}
function ClickBtn1(event:MouseEvent):void
{
trace("made it1");
}
function ClickBtn2(event:MouseEvent):void
{
trace("made it2");
}
......
Use:
for(var i:int=1; i<10; i++) {
this["P000"+String(i)].addEventListener(MouseEvent.CLICK, this["ClickBtn"+String(i)]);
}
Copy link to clipboard
Copied
Use:
for(var i:int=1; i<10; i++) {
this["P000"+String(i)].addEventListener(MouseEvent.CLICK, this["ClickBtn"+String(i)]);
}
Copy link to clipboard
Copied
Cheers!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now