A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Dispatch Event from one class to another

  1. #1
    Member
    Join Date
    Jun 2004
    Location
    London, UK
    Posts
    57

    Dispatch Event from one class to another

    Hi,

    Could someone point me to a simple example of how to dispatch an event from one class that another class is able to listen to (or do you always have to create an instance of the class which dispatches the event within any class that wants to be notified?).

    In a nutshell, I want a few different classes to react to (listen for) a particular event.

    Thanks
    Robin.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You simply need to add the event listeners to the dispatching instance. Stage is a convenient one for this example. Let's say you have two classes, Alice and Bob, which want to listen for stage mouse down events.
    Code:
    var a:Alice = new Alice();
    var a2:Alice = new Alice();
    var b:Bob = new Bob();
    addChild(a);
    addChild(a2);
    addChild(b);
    Alice:
    Code:
    public class Alice extends Sprite{
      public function Alice(){
         this.addEventListener(Event.ADDED_TO_STAGE, init);
      }
    
      public function init(event:Event){
         stage.addEventListener(MouseEvent.MOUSE_DOWN, mdfunction);
      }
      
      public function mdfunction(event:MouseEvent){
         trace("this is an Alice mousedown");
      }
    }
    and do the same for Bob. Then when you click on the stage, you should see the traces:
    this is an Alice mousedown
    this is an Alice mousedown
    this is a Bob mousedown

    The traces may be in a different order.

  3. #3
    Member
    Join Date
    Jun 2004
    Location
    London, UK
    Posts
    57
    Many thanks for that.. will give it a try.

  4. #4
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,136
    That doesn't really explain how to dispatch a custom event.
    The best way to do it is to make your class implement IEventDispatcher. Then you can dispatch custom events from anywhere in your class like this:

    this.dispatchEvent(new Event("something"));

    Then from outside you can add a listener for "something":

    myClass.addEventListener("something",someFunctionT oDo);

    Implementing IEventDispatcher does have some overhead but it's a very clean way to do it without creating and referencing singleton custom event classes or something like that.

  5. #5
    Member
    Join Date
    Jun 2004
    Location
    London, UK
    Posts
    57
    Hi, I'm still not getting this quite right.. can anyone see why the Receiving class doesn't receive notification from the dispatching class?
    Thanks.

    Code:
    // DOCUMENT CLASS
    package 
    {
    	import flash.display.*;
    	import flash.events.*;
    
    	public class Base extends Sprite
    	{
    		public function Base()
    		{
    			var d:DispatchingClass = new DispatchingClass();
    			var r:ReceivingClass = new ReceivingClass();
    		}
    	}
    }
    
    // CLASS WHICH SENDS THE EVENT
    package 
    {
    	import flash.events.*;
    
    	public class DispatchingClass extends EventDispatcher
    	{
    		public static const SOME_EVENT:String = "someEvent";
    
    		public function DispatchingClass()
    		{
    			dispatchEvent(new Event("SOME_EVENT"));
    			trace("should have been dispatched"); // ok
    		}
    	}
    }
    
    // CLASS TO RECEIVE THE EVENT NOTIFICATION
    package 
    {
    	import flash.display.*;
    	import flash.events.*;
    	
    	public class ReceivingClass extends Sprite
    	{
    		public function ReceivingClass()
    		{
    			addEventListener(DispatchingClass.SOME_EVENT, someEventListener);
    			trace("listener added");// ok
    		}
    
    		private function someEventListener(event:Event):void
    		{
    			trace("event reveived"); // <<<< THIS NEVER OCCURS???
    		}
    	}
    }

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    There are three big issues with that code. First, you have the SOME_EVENT constant defined as "someEvent", but actually dispatch an event using the string "SOME_EVENT" instead of the constant.

    Second, you have the dispatching class doing the dispatch only in the constructor, which means it'll only happen once and that before any listeners could have been added.

    And third, you added the listener to the receiving class, not the dispatching class. You add the listener IN the reciever TO the dispatcher.

    Let me attempt to fix:
    Code:
    // DOCUMENT CLASS
    package 
    {
    	import flash.display.*;
    	import flash.events.*;
    
    	public class Base extends Sprite
    	{
    		public function Base()
    		{
    			var d:DispatchingClass = new DispatchingClass();
    			var r:ReceivingClass = new ReceivingClass();
                            r.setup(d);
    		}
    	}
    }
    
    // CLASS WHICH SENDS THE EVENT
    package 
    {
    	import flash.events.*;
            import flash.utils.Timer;
    
    	public class DispatchingClass extends EventDispatcher
    	{
    		public static const SOME_EVENT:String = "someEvent";
    
    		public function DispatchingClass()
    		{
                       //set up timer to call beat every 5 seconds
                       var t:Timer = new Timer(5000);
                       t.addEventListener(TimerEvent.timer, beat); //t dispatches the timer event
    		}
    
                   public function beat(event:TimerEvent):void{
    			dispatchEvent(new Event(SOME_EVENT)); //this dispatches SOME_EVENT
    			trace("should have been dispatched"); // ok
                   }
    	}
    }
    
    // CLASS TO RECEIVE THE EVENT NOTIFICATION
    package 
    {
    	import flash.display.*;
    	import flash.events.*;
    	
    	public class ReceivingClass extends Sprite
    	{
    		public function ReceivingClass()
    		{
    		}
    
                    public function setup(d:DispatchingClass){
    			d.addEventListener(DispatchingClass.SOME_EVENT, someEventListener);
    			trace("listener added");// ok
                    }
    
    		private function someEventListener(event:Event):void
    		{
    			trace("lub dub"); 
    		}
    	}
    }
    Last edited by 5TonsOfFlax; 08-23-2007 at 10:30 AM. Reason: event listeners take event arguments. duh.

  7. #7
    Member
    Join Date
    Jun 2004
    Location
    London, UK
    Posts
    57
    Perfect.. Thank you!

    ps. If anyone else uses this example timer should be upper case (and started).

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