Flash Gallery | Flash Templates | Flash Menu | Flash Design | Flash Audio & Video

flash page flip

Actionscript 3.0 video tutorials

+ Reply to Thread
Results 1 to 1 of 1

Thread: Custom events - EventDispatcher

  1. #1
    Administrator Living At The FlepStudio! Flep is on a distinguished road
    Join Date
    Jul 2007
    Posts
    5,452
    Rep Power
    8

    Custom events - EventDispatcher

    amazing Flash templates
    After varied tutorials and articles and after as many free utilities free available with the source codes, whoever studied deeply the flash.events package will have now surely a good familiarity with the methods addEventListener and removeEventListener of Actionscript 3.0.
    Having said this, the moment has come to take a closer look at how to create some personalized events with Actionscript 3.0... personalized events'

    Yes, with Actionscript 3.0 (we could already do it with Actionscript 2.0) we can create some events that do not belong to the flash.events package.
    Better, in this tutorial we will see how to add an event to the built-in Event class, creating our own class, subclass of DispatchEvent, that extends the DispatchEvent and inherits its methods and properties.
    As an example, we know that the Event class has an event called ENTER_FRAME . We will add to that class the events EVENTO_UNO and EVENTO_DUE and use them as we wish.
    Let us see how to do it' I create a FLA and save it as 'main.fla'.
    Into which, I create a MovieClip of whatever shape you want and place one of its instance on stage named 'clip_mc'.
    I create the Document Class, an AS file saved as 'Manager.as', implemented the following way:
    Code:
    package
    {
    	import flash.display.MovieClip;
    	import flash.events.Event;
    	
    	public class Manager extends MovieClip
    	{
    		private var movement:Movement;
    		
    		public function Manager()
    		{
    			init();
    		}
    		
    		private function init():void
    		{
    			stage.frameRate=31;
    			
    			movement=new Movement(this);
    			movement.addEventListener(Movement.EVENTO_UNO,partito);
    			movement.addEventListener(Movement.EVENTO_DUE,arrivato);
    			movement.addEventListener(Movement.EVENTO_TRE,tornato);
    			movement.parti();
    		}
    		
    		private function partito(e:Event):void
    		{
    			trace('partito');
    		}
    		
    		private function arrivato(e:Event):void
    		{
    			trace('arrivato');
    		}
    		
    		private function tornato(e:Event):void
    		{
    			trace('tornato');
    		}
    	}
    }
    Let us analyse the code

    Properties

    An instance of the Movement class that we will create next
    private var movement:Movement;

    Methods
    init();
    I impost the frame rate
    stage.frameRate=31;
    I create the instance of the Movement class
    movement=new Movement(this);
    Next, is an important part of this tutorial. As we can see, I add a listener to the 'movement' instance. We know that addEventListener needs two parameters: an event and the name of the method to be called when the event is reached.
    In a normal situation, we would have the following syntax:
    my_mc.addEventListener(Event.ENTER_FRAME,partito);
    instead, to create our own personalised event at the top of the Event class, we pass as parameter a static property of the Movement class (seen next) and the name of the method ('partito'). So as normal, at a chosen event, the respective method will be called.
    movement.addEventListener(Movement.EVENTO_UNO,part ito);
    movement.addEventListener(Movement.EVENTO_DUE,arri vato);
    movement.addEventListener(Movement.EVENTO_TRE,torn ato);
    I call a method of the Movement class which as we can see, it is not a static method as it is called from the instance of the class itself
    movement.parti();

    Let us look at the Movement class.

    I create an AS file and save it as 'Movement.as', implemented the following way:
    Code:
    package
    {
    	import flash.display.MovieClip;
    	import flash.events.EventDispatcher;
    	import flash.events.Event;
    	
    	public class Movement extends EventDispatcher
    	{
    		private var _fla:MovieClip;
    		
    		private var arrX:int=400;
    		
    		public static const EVENTO_UNO:String='eventoUno';
    		public static const EVENTO_DUE:String='eventoDue';
    		public static const EVENTO_TRE:String='eventoTre';
    		
    		public function Movement(fla:MovieClip)
    		{
    			_fla=fla;
    		}
    		
    		public function parti():void
    		{
    			dispatchEvent(new Event(Movement.EVENTO_UNO));
    			_fla.clip_mc.addEventListener(Event.ENTER_FRAME,go);
    		}
    		
    		private function go(e:Event):void
    		{
    			var dx:Number=arrX-e.target.x;
    			var ax:Number=dx*.2;
    			e.target.x+=ax;
    			if(Math.abs(dx)<=.2)
    			{
    				e.target.x=arrX;
    				dispatchEvent(new Event(Movement.EVENTO_DUE));
    				e.target.removeEventListener(Event.ENTER_FRAME,go);
    				arrX=25;
    				e.target.addEventListener(Event.ENTER_FRAME,back);
    			}
    		}
    		
    		private function back(e:Event):void
    		{
    			var dx:Number=arrX-e.target.x;
    			var ax:Number=dx*.2;
    			e.target.x+=ax;
    			if(Math.abs(dx)<=.2)
    			{
    				e.target.x=arrX;
    				dispatchEvent(new Event(Movement.EVENTO_TRE));
    				e.target.removeEventListener(Event.ENTER_FRAME,back);
    			}
    		}
    	}
    }
    Let us analyse the code

    To remember: the Movement class extends the built-in class DispatchEvent
    Properties
    An instance MovieClip to which I assign the root passed by the Document class (Manager.as) to that I can retrace 'clip_mc' placed on stage.
    private var _fla:MovieClip;
    a numerical variable that contains the value of x at arrival during the movement of 'clip_mc' using the inertia effect.
    private var arrX:int=400;
    Next are the three static and public properties of the class. When we added the listeners to the instance of this class in the Document class, we done it calling those 3 properties containing a string value (as example, the buit-in Event.ENTER_FRAME has the string value 'enterFrame')
    public static const EVENTO_UNO:String='eventoUno';
    public static const EVENTO_DUE:String='eventoDue';
    public static const EVENTO_TRE:String='eventoTre';

    Methods
    parti();
    from the Document Class, this method is called to start the animation of 'clip_mc'(an x axis movement using the inertia effect). Before to start the ENTER_FRAME, we dispatch EVENTO_UNO. Doing so, we shall not forget that the Document class has a listener added to that event, so the method will be called when the listener is added ('partito').
    dispatchEvent(new Event(Movement.EVENTO_UNO));
    _fla.clip_mc.addEventListener(Event.ENTER_FRAME,go );

    go();
    here I apply an inertia effect to the x axis of 'clip_mc'
    var dx:Number=arrX-e.target.x;
    var ax:Number=dx*.2;
    e.target.x+=ax;
    if(Math.abs(dx)<=.2)
    {
    and at the right time, I impost the 'clip_mc' x equal to the value of arrX
    e.target.x=arrX;
    I dispatch another event (EVENTO_DUE) that at its turn will be revealed by the Document class which has a listener waiting for such event and will call the respective method (arrivato)
    dispatchEvent(new Event(Movement.EVENTO_DUE));
    I remove the ENTER_FRAME
    e.target.removeEventListener(Event.ENTER_FRAME,go) ;
    I change the point of arrival
    arrX=25;
    I add another ENTER_FRAME that will call the method 'back'
    e.target.addEventListener(Event.ENTER_FRAME,back);
    }

    back();
    another effect of movement on 'clip_mc' x axis
    var dx:Number=arrX-e.target.x;
    var ax:Number=dx*.2;
    e.target.x+=ax;
    if(Math.abs(dx)<=.2)
    {
    at the right moment, I impost the 'clip_mc' x equal to the value of arrX
    e.target.x=arrX;
    I dispatch another event (EVENTO_TRE) which will be revealed by the Document Class which has a listener waiting for such event and will then call the respective method ('tornato')
    dispatchEvent(new Event(Movement.EVENTO_TRE));
    I remove the ENTER_FRAME
    e.target.removeEventListener(Event.ENTER_FRAME,bac k);
    }

    This is the final result:






    Source files:
    Attached Files

+ Reply to Thread

Similar Threads

  1. Focus events
    By xFlashStudentx in forum advanced Actionscript 3.0
    Replies: 0
    Last Post: 26-10-09, 02:58
  2. Custom events and describeType method
    By Flep in forum Tutorials
    Replies: 0
    Last Post: 19-10-09, 05:07
  3. Please Help with EventDispatcher
    By sututuyet in forum advanced Actionscript 3.0
    Replies: 0
    Last Post: 28-11-08, 03:07
  4. custom cursor under a box
    By Glen Charles Rowell in forum Flash English
    Replies: 1
    Last Post: 03-07-08, 14:16
  5. chi mi illumina sulla classe EventDispatcher
    By mariano.martucci in forum Actionscript 3.0 base
    Replies: 2
    Last Post: 06-08-07, 11:17

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Optimization by vBSEO