1 Reply Latest reply on Jan 6, 2010 9:20 AM by Rothrock

    AsBroadcaster vs. EventDispatcher

    Rothrock Level 5

      I'm making some elearning courses and we are trying to combine semi-automation with the ability to place artwork and content on the timeline. Basically we have a series of small tasks -- read this, click this button, see the result, and so on. We have a "highlight" area component that indicates the area a user is to click. Currently that component dispatches and event from the instance when it is clicked and the main timeline is listening for the event and then moves them ahead to the next task when they have clicked properly.

       

      The issue is that we have lots of these to build and lots of different people (with different code styles and skill levels) work on them and they are frequently edited and rearanged to boot. So the following code:

       

      myClickAreaInstance.addEventListener("clicked",handleClick)

       

      and the handleClick function and removing the listener all has to be handled on the timeline. And for a given task there might be an intro animation and the click area component might not come in until the end. So where to put that code and how to automate it becomes tricky. So I first dug up AsBroadcaster and could use it sort of like Mouse or Key classes.

       

           AsBroadcaster.initialize(ClickArea)

           ClickArea.addListener(this);

           function clicked(obj){

                //handle click broadcast

           }

       

      And then in the class

       

           ClickArea.broadcastMessage("click",{someObj});

       

      But then I asked myself if I could initialize the Class instead of just the instances with AsBroadcaster why couldn't I do the same thing with EventDispatcher. And sure enough it worked!

       

           EventDispatcher.initialize(ClickArea);

           ClickArea.addEventListener("clicked",handleClick);

           function handleClick(e:Object){

                //handle event

           }

       

      And then in the class dispatching from the static class method:

       

           ClickArea.dispatchEvent({type:"click",target:this});

       

      They both seem to work just fine.

       

      So I'm wonderinging if anybody else has used this approach?

      Is it sound?

      Are there hidden gotchas?

      Would it be better to us AsBroadcaster or EventDispatcher?

        • 1. Re: AsBroadcaster vs. EventDispatcher
          Rothrock Level 5

          I'm currently leaning toward AsBroadcaster. It seems that if the timeline loops and I addEventListener again then each dispatch will happen twice. But if I use broadcaster it only goes out once.

           

          I know that we should be good and removeEventListeners, but there are times when that is difficult. And there are times when it is beyond my control!

           

          So any ideas.