Event listeners are not working if declared without event argument

Dimitri's Avatar

Dimitri

03 Jul, 2011 11:21 AM

I'm not sure if it is a bug, but this will work:

   override public function onRegister():void
    {
        addContextListener(ApplicationEvent.PING, answerThePing);
    }

    public function answerThePing(e:Event):void
    {
        _log.debug("answering the ping");
    }

And this will not:

   override public function onRegister():void
    {
        addContextListener(ApplicationEvent.PING, answerThePing);
    }

    public function answerThePing():void
    {
        _log.debug("answering the ping");
    }

I think it would be awesome to have an opportunity to declare event listeners without e:Event - after all, pretty often I don't need it.

  1. Support Staff 1 Posted by Stray on 03 Jul, 2011 12:18 PM

    Stray's Avatar

    Hi Dimitri, that's a requirement of AS3 event listeners. The eventMap only handles the registering of your listener, it doesn't do anything fancy beyond that, so it's not a bug, just a characteristic of AS3.

  2. 2 Posted by Dimitri on 03 Jul, 2011 12:22 PM

    Dimitri's Avatar

    Oh yeah, sure.
    Then I'd suggested to write about it explicitly somewhere (Best Practices?), or throw some kind of error in runtime. It took me about 2hrs for to figure out why my Mediator stopped to receive events.

    Thanks for a quick reply. =)

  3. 3 Posted by Stray on 03 Jul, 2011 12:27 PM

    Stray's Avatar

    Hi Dimitir,

    it should have thrown a runtime error - along the lines of "the function wasn't expecting an argument and one was received" - this is built in to AS3.

    I frequently forget to add the event to the handler and always see this error. It's not thrown by robotlegs but by the flash player. To throw it from robotlegs we'd have to do some sort of reflection on the handler which would be really performance sapping.

    The reason it isn't mentioned in the best practices is probably just that it's normal AS3 practice to have to declare the event in a handler... are you new to AS3? I'm not aware of it having confused anyone in the past - which doesn't mean that it hasn't, they might just not have brought it to our attention.

    I'll just confirm that I do still see the 'function args' error myself...

    Stray

  4. Support Staff 4 Posted by Stray on 03 Jul, 2011 12:32 PM

    Stray's Avatar

    Hi Dimitri - I just confirmed that it does throw an error - for example:

    ArgumentError: Error #1063: Argument count mismatch on 
    mosaic.view::MosaicToolViewMediator/showGridOnView(). Expected 0, got 1.
    

    The only reason you wouldn't see the error is if you were wrapping something in a try {} block, or if you are 'testing' in browser using your non-debug release of the swf... which would be a really strange thing to do!

    It sounds to me like your development environment or practice is broken - it's certainly nothing to do with robotlegs if you're not seeing that error message.

    Can you confirm what you're doing that doesn't produce the error?

    Thanks,

    Stray

  5. 5 Posted by Dimitri on 03 Jul, 2011 12:35 PM

    Dimitri's Avatar

    Uhm, well, no, I'm no new to AS3, actually coding since Macromedia Flash 4.

    I'm pretty much aware of requirement for event handler to have event argument, i'm just new to RL, and see these listeners, not exactly as listeners, but more like processors. You can see it by naming, with EventListeners, i'd used something like 'onApplicationPing' instead of 'answerThePing'. Sorry for that mental glitch. =)

    Regarding throwing an error by FP - crap, just came on my mind - that's because of Chrome built-in player. =( Have to install debug version. Sorry. Anyway - you're right, it has nothing to do with RL. Thank you a lot. =)

  6. 6 Posted by Stray on 03 Jul, 2011 05:42 PM

    Stray's Avatar

    I expect a lot of people may have that mental glitch and then they get their first "duh, you missed the event parameter" error and it clears it up... so I'll make a note to add it to the getting-started docs.

    Ah - naughty chrome player! I'm glad you got it sorted.

    Stray

  7. Ondina D.F. closed this discussion on 01 Nov, 2011 04:13 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac