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

Several buttons and dynamic(?) EventListener

Explorer ,
May 13, 2014 May 13, 2014

Hi everyone,

I place 10 buttons on my stage, name it "b1", "b2", "b3",.. , "b10".

All these buttons should use the same function.

Is it possible to do it dynamically?

var a: Array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

for (var i = 0; i < a.length; i++) {

button = "b" + a;

  this[button].addEventListener(MouseEvent.CLICK, onClick_b);

  function onClick_b(pEvt: Event): void {

  my_mc.play();

  }

}

Only the button "b10" plays the function.

Anyone an idea how to let all the other buttons play this function?

Thank you! C.

TOPICS
ActionScript
553
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

Guru , May 14, 2014 May 14, 2014

what you want is this:

for (var i = 0; i < a.length; i++) {

    getChildByName("b" + a).addEventListener(MouseEvent.CLICK, onClick_b);

     }

Translate
Guide ,
May 13, 2014 May 13, 2014

Assuming all your buttons are Button symbols, and hence are SimpleButtons and they all exist at the point you run this code:

var loops:numChildren;

for (var i:int=0; i<loops; i++) {

     var btn:SimpleButton = getChildAt(i) as SimpleButton;

     if (btn) {

          btn.buttonMode = true;

          btn.addEventListener(MouseEvent.CLICK, onButtonClick);

     }

}

//note the function is defined outside the for loop

function onButtonClick(e:Event):void {

     if (my_mc) {

          my_mc.play();

     }

}

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
Explorer ,
May 14, 2014 May 14, 2014

Thank you for your answers, this solution works, but I need an array in my project...

Now my buttons are called "bone", "btwo", "three".

var button: String;

button = "b";

var a: Array = ["one","two","three"];

for (var i = 0; i < a.length; i++) {

    button = "b" + a;

     button.addEventListener(MouseEvent.CLICK, onClick_b);

     }

function onClick_b(pEvt: Event): void {

    my_mc.play();

    }

But it doesn't work...

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
Guru ,
May 14, 2014 May 14, 2014

var button: String;

...

button.addEventListener(MouseEvent.CLICK, onClick_b);


You can`t add en eventlistener to a string

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
Explorer ,
May 14, 2014 May 14, 2014

Hi mm, I tried this:

this[button].addEventListener(MouseEvent.CLICK, onClick_b);

and it works for the button "bthree".

But the other buttons won't work.

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
Guru ,
May 14, 2014 May 14, 2014
LATEST

what you want is this:

for (var i = 0; i < a.length; i++) {

    getChildByName("b" + a).addEventListener(MouseEvent.CLICK, onClick_b);

     }

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