AS3 pass arguments in addEventListener
A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: AS3 pass arguments in addEventListener

  1. #1
    Member
    Join Date
    Nov 2004
    Location
    London, Covent Garden
    Posts
    78

    AS3 pass arguments in addEventListener

    Hi, i gather it's not possible to send arguments from Mouse Events without writing a custom EventManager. What would be a correct way to identify either the object sending the event or to pass arguments to a function specified in the Mouse Event?

    I discovered this but I'm not sure if it's appropriate since i'm not familiar yet with EventManager/Dispatch stuff, I don't know what I'm looking for:
    http://www.kirupa.com/forum/showthread.php?t=291250

    Hypothetical:
    I have a function which receives a MovieClip object then adds a listener to it - I want to then pass the same object onto the next function if the object receives a mouse click.

    Code:
    //ADD EVENT LISTENERS
    public static function addFirstClickListener(myButton:MovieClip) {
    	myButton.addEventListener(MouseEvent.CLICK, doSomething(myButton));
    }

    PS: trace for "myButton" turns out correctly
    button passed is [object news]
    Last edited by bosie; 05-31-2008 at 03:14 PM.
    bosie

  2. #2
    Mod cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,440
    - The right of the People to create Flash movies shall not be infringed. -
    | www.Flashscript.biz | Flashscript Biz Classes/Components |

  3. #3
    Junior Member
    Join Date
    Jun 2008
    Posts
    3
    hello everyone, i'm a young starter(19) and i found this with a bit of a mix of javascript , however i don't know how to access evt.target in the function then... and don't know how to remove the listener also afterwards

    PHP Code:
    addEventListener(MouseEvent.CLICK,function() {klik("functiontest")});

    public function 
    klik(s:String):void {
                
    trace(s);
                
            } 

  4. #4
    5+5=55 Schfifty Five's Avatar
    Join Date
    Jun 2006
    Posts
    698
    Quote Originally Posted by jensmct
    hello everyone, i'm a young starter(19) and i found this with a bit of a mix of javascript , however i don't know how to access evt.target in the function then... and don't know how to remove the listener also afterwards

    PHP Code:
    addEventListener(MouseEvent.CLICK,function() {klik("functiontest")});

    public function 
    klik(s:String):void {
                
    trace(s);
                
            } 

    Try something like:

    PHP Code:
    addEventListener(MouseEvent.CLICK,mouseClick);
    public function 
    mouseClick(event:MouseEvent){
    klik("functiontest",event);
    }

    public function 
    klik(s:String,event:Event):void {
        
    trace(s);
        
    trace(event.target);

    To remove the listener, use:

    PHP Code:
    removeEventListener(MouseEvent.CLICK,mouseClick); 
    (Sorry about not answering the original post, but I think cancerinform's link should help with that)

  5. #5
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    55 that's no good...you're not actually sending a variable, just calling a second function with the same hard-coded var in it...

    Flax dropped some cool closure code on this problem a while back.

  6. #6
    5+5=55 Schfifty Five's Avatar
    Join Date
    Jun 2006
    Posts
    698
    Quote Originally Posted by neznein9
    55 that's no good...you're not actually sending a variable, just calling a second function with the same hard-coded var in it...

    Flax dropped some cool closure code on this problem a while back.
    I know, but for what jensmct asked, it should work fine.

  7. #7
    Junior Member
    Join Date
    Jun 2008
    Posts
    3
    thx people but i already knew that, just searching for a new way to adress your problem

  8. #8
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Whoops missed the first post :P

    All these solutions are overkill for that specific problem - there are two attributes of Event that will give you what you need:

    PHP Code:
    myButton.addEventListener(MouseEvent.CLICKdoSomething);

    private function 
    doSomething(e:MouseEvent):void{
        
    trace(e.currentTarget);     //  myButton
        
    trace(e.target);     //  myButton or a child of it

    Event.currentTarget will give you the object that you attached the listener to - so for that code it would always be a reference to myButton.

    Event.target will give you the object that triggered the event - so if myButton had a movieclip and a textfield inside it, you might get either object as the target depending on which _part_ of the button you clicked. It's a subtle distinction but it can save you a lot of headaches.

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

Poll by Flashkit.com