A Flash Developer Resource Site

Page 2 of 2 FirstFirst 12
Results 21 to 28 of 28

Thread: Passing args with event ?

  1. #21
    Member
    Join Date
    May 2007
    Location
    Poland
    Posts
    65
    Yes, overloading is one thing that is missing in AS3... maybe AS4?

  2. #22
    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); 
                 
            } 

  3. #23
    Junior Member
    Join Date
    Mar 2003
    Location
    Quebec !
    Posts
    9
    Hi,

    long time no write... actualy I wouldn't use this method anymore

    But anyway, you could alwats use a weak listener (see addEventListener function)
    OULALALALLALAOUAOUALALLALL...

    I hate signature

  4. #24
    Junior Member
    Join Date
    Mar 2009
    Posts
    1
    Thanks a lot that listener with arguments is great.

  5. #25
    Frash Deveropa
    Join Date
    Nov 2013
    Posts
    2
    To anyone that ended here looking for a simple solution to a problem like Andreq85's, it does exist:

    PHP Code:
    var someHandlerFunc:Function = someFunc(someArgs);
    myBtn.addEventListener(MouseEvent.CLICKsomeHandlerFunc);
    //myBtn.removeEventListener(MouseEvent.CLICK, someHandlerFunc);

    function someFunc(someArgs:Object):Function {
        return function(
    e:MouseEvent):void {
            
    trace(someArgs);
        };

    For multiple elements you can do:

    PHP Code:
    btn1.addEventListener(MouseEvent.CLICKaddPage(1));
    btn2.addEventListener(MouseEvent.CLICKaddPage(2));
    btn3.addEventListener(MouseEvent.CLICKaddPage(3));

    function 
    addPage(_nPage:int):Function {
        return function(
    e:MouseEvent):void {
            
    nPage += _nPage;
        };


  6. #26
    Senior Member
    Join Date
    Aug 2002
    Location
    Dublin, Ireland
    Posts
    1,749
    Quote Originally Posted by metafurionx View Post
    To anyone that ended here looking for a simple solution to a problem like Andreq85's, it does exist:

    PHP Code:
    var someHandlerFunc:Function = someFunc(someArgs);
    myBtn.addEventListener(MouseEvent.CLICKsomeHandlerFunc);
    //myBtn.removeEventListener(MouseEvent.CLICK, someHandlerFunc);

    function someFunc(someArgs:Object):Function {
        return function(
    e:MouseEvent):void {
            
    trace(someArgs);
        };

    For multiple elements you can do:

    PHP Code:
    btn1.addEventListener(MouseEvent.CLICKaddPage(1));
    btn2.addEventListener(MouseEvent.CLICKaddPage(2));
    btn3.addEventListener(MouseEvent.CLICKaddPage(3));

    function 
    addPage(_nPage:int):Function {
        return function(
    e:MouseEvent):void {
            
    nPage += _nPage;
        };

    Nifty trick for a one-off, and for lazy prototyping, but I think it would be a bit risky for a production system as it makes clean up code rather tough eg: it's rather tough to find a function reference to removeEventListener!

    All the same, thanks for some food for thought.

  7. #27
    Frash Deveropa
    Join Date
    Nov 2013
    Posts
    2
    Quote Originally Posted by AlsoGaiusCoffey View Post
    Nifty trick for a one-off, and for lazy prototyping, but I think it would be a bit risky for a production system as it makes clean up code rather tough eg: it's rather tough to find a function reference to removeEventListener!

    All the same, thanks for some food for thought.
    "Nifty trick"? "One-off"? "Lazy prototyping"? "A bit risky"? "Rather tough"? "Some food for thought"?
    Wow, you desperately tried to repress the solution I just offered. That was extremely rude.

    No, I didn't make this up. The approach is solid and widely used, let alone it being ancienter than this thread.
    stackoverflow.com - To pass a parameter to event listener in AS3 the simple way… does it exist?
    actionscript.org - adding parameters to function in addEventListener
    kirupa.com - How to pass arguments from an event listener in AS3 - SIMPLE
    life.neophi.com - Removing Anonymous Event Listeners

    Quote Originally Posted by AlsoGaiusCoffey View Post
    it's rather tough to find a function reference to removeEventListener
    PHP Code:
    btn1.addEventListener(MouseEvent.CLICKaddPage(1));
    btn2.addEventListener(MouseEvent.CLICKaddPage(2));
    btn3.addEventListener(MouseEvent.CLICKaddPage(3));

    function 
    addPage(_nPage:int):Function {
        return function(
    e:MouseEvent):void {
            
    nPage += _nPage;
            
    e.currentTarget.removeEventListener(e.typearguments.callee); // Run once and remove itself
        
    };

    PHP Code:
    function addPage(_nPage:int):Function {
        return function(
    e:MouseEvent):void {
            
    nPage += _nPage;
        };
    }

    var 
    add1:Function = addPage(1);
    var 
    add2:Function = addPage(2);
    var 
    add3:Function = addPage(3);
    btn1.addEventListener(MouseEvent.CLICKadd1);
    btn2.addEventListener(MouseEvent.CLICKadd2);
    btn3.addEventListener(MouseEvent.CLICKadd3);

    // Remove when you need to
    btn1.removeEventListener(MouseEvent.CLICKadd1);
    btn2.removeEventListener(MouseEvent.CLICKadd2);
    btn3.removeEventListener(MouseEvent.CLICKadd3); 
    Is this rather tough for you?

  8. #28
    Senior Member
    Join Date
    Aug 2002
    Location
    Dublin, Ireland
    Posts
    1,749
    Certainly not intended the way you took it.

    For the record, though, "possible" is not the equivalent of "easy" just as "precedent" is not a synonym for "good practice".

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