A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: [RESOLVED] Can I add parameters to an event listener's function?

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    62

    resolved [RESOLVED] Can I add parameters to an event listener's function?

    I'm trying and it doesn't seem to like it. I'm guessing it's not possible?

    :

    Actionscript Code:
    bonus.addEventListener(Event.ADDED_TO_STAGE,addBonus(hitArray["xPos"],hitArray["yPos"])

  2. #2
    Member
    Join Date
    Mar 2009
    Posts
    62
    in fact ignore that, I missed a bracket off

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You did miss a bracket, but if you're trying what it looks like you're trying that won't work either.

    You are attempting to add the result of calling addBonus as the listener. Since the result is probably not a function which takes an event argument, that won't work.

    You can make a function which does return a function which can be used as a listener. I made a framework to make that easy. functionUtils

    The part you'd be interested in is closurizeEventHandler, which is a function which takes an event handler function that takes extra arguments, and values for those arguments, and returns a function that can actually be used as an event handler.

  4. #4
    Member
    Join Date
    Mar 2009
    Posts
    62
    Quote Originally Posted by 5TonsOfFlax View Post
    You did miss a bracket, but if you're trying what it looks like you're trying that won't work either.

    You are attempting to add the result of calling addBonus as the listener. Since the result is probably not a function which takes an event argument, that won't work.

    You can make a function which does return a function which can be used as a listener. I made a framework to make that easy. functionUtils

    The part you'd be interested in is closurizeEventHandler, which is a function which takes an event handler function that takes extra arguments, and values for those arguments, and returns a function that can actually be used as an event handler.
    I was just trying to call a function that had set up bonus_mc and added t the stage. The bonuses needed certain parameters such as the x value etc in order to add them. I wasn't trying to get a return value, but maybe the way I did it makes it seem as thought I was.

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I know you weren't trying to get a return value, but that's what you actually were doing.

    In your code, addBonus will be called at the time you attempt to add the listener, instead of as a listener. That's before the object is added to the stage, defeating the whole purpose of having it as an ADDED_TO_STAGE listener.

  6. #6
    Member
    Join Date
    Mar 2009
    Posts
    62
    Quote Originally Posted by 5TonsOfFlax View Post
    I know you weren't trying to get a return value, but that's what you actually were doing.

    In your code, addBonus will be called at the time you attempt to add the listener, instead of as a listener. That's before the object is added to the stage, defeating the whole purpose of having it as an ADDED_TO_STAGE listener.
    Ahh I see, thank you.

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