Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

dynamically create and addEventListener on mouse click

New Here ,
Jan 25, 2014 Jan 25, 2014

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");

}

......

TOPICS
ActionScript
416
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jan 25, 2014 Jan 25, 2014

Use:

for(var i:int=1; i<10; i++) {

  this["P000"+String(i)].addEventListener(MouseEvent.CLICK, this["ClickBtn"+String(i)]);

}

Translate
LEGEND ,
Jan 25, 2014 Jan 25, 2014

Use:

for(var i:int=1; i<10; i++) {

  this["P000"+String(i)].addEventListener(MouseEvent.CLICK, this["ClickBtn"+String(i)]);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 25, 2014 Jan 25, 2014
LATEST

Cheers!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines