dcsimg
A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: multiple event listeners

  1. #1
    Senior Member
    Join Date
    Aug 2005
    Location
    The Netherlands
    Posts
    326

    multiple event listeners

    In AS2 you could add an onRelease to multiple buttons like this:

    Code:
    onebutton.onRelease = anotherbutton.onRelease = lastbutton.onRelease = function() {
    //do something when either button is pressed;
    }
    Is there something similar possible in AS3 using addeventlistener? Besided copying the same addeventlistner line for all individual buttons?
    Last edited by digitalecartoon; 09-27-2009 at 04:33 PM.

    Illustration | Animation | Web Banners | Graphic Design
    Ducklord Studio

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I assume you meant in AS3. addEventListener returns void, so you can't do that directly. But you can assign your listener function to a variable of type Function and do the listeners. I'm guessing the point is having to change only one line when you want to change the listener?

    Code:
    var aListener:Function = someListenerFunction;
    
    onebutton.addEventListener(MouseEvent.MOUSE_UP, aListener);
    anotherButton.addEventListner(MouseEvent.MOUSE_UP, aListener);
    Of course, if you have a lot of buttons and you want to automate rather than copy the addEventListener line, you could put them in an array and process the array.
    Code:
    var buttons:Array = [oneButton, anotherButton, lastButton];
    for (var i:int = 0; i < buttons.length; i++){
      buttons[i].addEventListener(MouseEvent.MOUSE_UP, someListenerFunction);
    }
    If your buttons are instances of a class, you could replicate the AS2 way by using a listener which invokes a function property.

    Code:
    public class As2AlikeButton extends MovieClip{
      public var onRelease:Function = function(){}; //do nothing function
    
      public function As2AlikeButton(){
        addEventListener(MouseEvent.MOUSE_UP, mupFunction);
      }
    
      public function mupFunction(event:MouseEvent):void{
        onRelease();
      }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center