4 Replies Latest reply: Jan 22, 2008 11:39 AM by sneakyimp RSS

    Extending EventDispatcher with dynamic event types

    sneakyimp Community Member
      I'm currently trying to write my own class to extend the EventDispatcher class. My goal is to create a Service class which acts as a switchboard for incoming socket data. Incoming socket data is parsed to create an Event which is Dispatched to the appropriate Service object which dispatches the event to any registered listeners.

      The problem I'm having is that I'm confused about how to handle the event types -- the string parameter which is always supplied as the first argument to the addEventListener() function. In all the examples I've seen, the value supplied is a class constant like MouseEvent.CLICK or VideoEvent.READY. In the case of my application, I want the developer to define their own Event types without having to go and edit my class files. In other words, I'd like to use dynamic event types with my class--maybe something like the code I've attached.

      I'm not new to Actionscript but am very new to the EventDispatcher class. I'm wondering a bit what that first parameter really does in a strict programming sense -- i haven't seen it explained all that well in the piles of documentation I've read on the Adobe site so I don't really know if I need to define my own Event class or what and whether I have to define all those possible event type constants/strings in advance or what.

      Any help/advice would be much appreciated.
        • 1. Re: Extending EventDispatcher with dynamic event types
          Jamesabth Community Member
          You can dispatch an event from the class with dispatchEvent(new Event(params)); and then listen to myInstance.addEventListener("name", func);
          • 2. Re: Extending EventDispatcher with dynamic event types
            sneakyimp Community Member
            i don't understand what you mean?

            what goes in params?
            what object does myInstance refer to?
            and how does this help answer my question?
            • 3. Re: Extending EventDispatcher with dynamic event types
              Damon Edwards Community Member
              Look up EventDispatcher in the help docs. It gives a great explanation there.
              • 4. Extending EventDispatcher with dynamic event types
                sneakyimp Community Member
                I have looked it up here:
                Actionscript 3.0 Language Reference
                and it just says that the first arg to 'addEventListener' is "The type of event" with no further explanation at all.

                I have read this:
                Flash Quick Starts: Programming With Actionscript 3.0: Event handling
                It doesn't help much either. It tells me I need the appropriate event object which makes me wonder DO I NEED MY OWN CLASS? For that first ard to addEventListener, it merely says "Substitute the appropriate constant". Given that I want to use dynamic types (i think so anyway) that is not helpful information either. Every single example on that page uses a class constant for event type.

                It links to this which says "First, the name of the specific event you want to respond to. Once again, each event is affiliated with a specific class, and that class will have a special value predefined for each event--sort of like the event's own unique name, which you should use for the first parameter." Still no useful detail about what that arg does and the insistence on constants.

                The quick start page also has a link title "Handling Events" in Programming ActionScript 3.0 but it links to some documentation titled "Working with XML". The only discussion of events in that 10 or so pages is because some user posted a comment.

                I have read this entire article which is more helpful than anything in the flash docs but still doesn't answer all my questions which (AGAIN) are:

                1) What is the real function of the first argument to addEventListener? I have not seen a clear explanation anywhere.
                2) Do I need to write my own Event class?
                3) What about dynamic event types that are defined at runtime? Are there any pitfalls or risks that arise from using a string instead of a predefined constant as the first param to addEventListener() ?

                Please stop telling me to read the documentation. I've been reading it for days.