What what what?  Who's listening?

Now that we've managed to send events to our EventManager class, what does it matter?  We need some objects to listen in to make this worthwhile don't we?  Indeed we do, so let's show two objects that are going to listen in for these events.

package com.pj_co.balloongame
{
        
        import com.pj_co.balloongame.BalloonEvent;
        import com.pj_co.managers.EventManager;
        
        /**
        * We Need to keep track of the score to know how the player has done!
        */
        public class ScoreCard
        {
                private var score : int = 0;
                
                public function ScoreCard ( )
                {
                        
                        EventManager.getInstance().addEventListener( BalloonEvent.POP, updateScore ) ;
                        
                }
                
                private function updateScore ( e : BalloonEvent ) : void
                {
                        score ++;
                }
        }
}


Above was our ScoreCard class, which obviously would need to know when a balloon was popped.  Below let's look at the LevelManager class which also needs to know.

package com.pj_co.balloongame
{
        
        import com.pj_co.balloongame.BalloonEvent;
        import com.pj_co.managers.EventManager;
        
        /**
        * We Need to keep track of the number of enimies killed till we can advance to the next level! 
        */
        public class LevelManager
        {
                private var score : int = 0;
                private var numBalloons : int = 15;
                
                public function LevelManager ( )
                {
                        
                        EventManager.getInstance().addEventListener( BalloonEvent.POP, updateScore ) ;
                        
                }
                
                private function updateScore ( e : BalloonEvent ) : void
                {
                        score ++;
                        if ( score > numBalloons )
                        {
                                // win!
                        }
                }
        }
}

Notice that all these two objects had to do was to listen to the EventManager.  This is really the key of what makes centralized event management powerful.  If you remember our object and event diagrams from before, can you imagine what they might look like now with such a system?




Alright now we're talking.  Looks a bit cleaner right?  So we know how to register as a dispatcher, we know how to listen in, and we can see now how many objects can more easily listen to a single place without knowing where the object is that's making the dispatch.  So what's next?  Read on for a bit about how this works behind the scenes and why the clone() method is important.  Then finally we'll talk about uses and advanced implementations that will be coming in part II of this tutorial.