2 Replies Latest reply on Jun 26, 2008 12:59 AM by sanjeev rajput

    custom event in as3

    sanjeev rajput
      Custom event example in as3
      Hi,

      Event listener model in cs3 looks nice it is not same as we were heaving earlier in as2 or till flash 8.

      Here is an example of using and making your own custom event in as3.
      This example allow user to load n number of XML file when XML file is loaded then dispatch an event "XMLLoaded" which can be listen by any other class any where.

      There are two class
      1. CustEvent
      2. DEvt

      Here are the definition of both class

      CustEvent.as

      /************************************************** *************************************
      * Author - Sanjeev Rajput
      * Date - 16-July-07
      * class is used to load any XML file and dispatch an event when XML is loaded
      ************************************************** ***************************************/
      package eventDispatch{
      import flash.events.Event;
      public class CustEvent extends Event {
      public static const XMLLoaded:String = "XMLLoaded";// Event Name
      public var XMLData:XML // loaded XML data
      public var XMLRef:String // XML file name
      public function CustEvent(type:String, param:String,param1:XML) {
      this.XMLData= param1;
      this.XMLRef=param;
      super(type);
      }
      }
      }
      DEvt.as

      /************************************************** *************************************
      * Author - Sanjeev Rajput
      * Date - 16-July-07
      * class is used to load any XML file and dispatch an event when XML is loaded
      ************************************************** ***************************************/
      package eventDispatch{
      import flash.events.EventDispatcher;
      import flash.net.URLLoader;
      import flash.events.Event;
      import flash.net.URLRequest;
      public class DEvt extends EventDispatcher {
      private var xmlLdr:URLLoader;
      private var urlr:URLRequest;
      private var xmlpath_str:String;
      public var XMLData:XML;
      private var counter:int=0;
      private var xmlRequestArr:Array
      public function DEvt():void {
      this.xmlRequestArr=new Array()
      }
      public function loadXML(fileRef:String,xmlRef:String):void {
      this.xmlpath_str=fileRef;
      this.xmlRequestArr.push(xmlRef)
      this.xmlLdr = new URLLoader();
      this.urlr=new URLRequest(this.xmlpath_str);
      this.xmlLdr.addEventListener(Event.COMPLETE, completeHandler);
      this.xmlLdr.load(this.urlr);
      }
      private function completeHandler(evt:Event) {
      this.XMLData=new XML(evt.target.data);
      this.dispatchEvent(new CustEvent(CustEvent.XMLLoaded,this.xmlRequestArr[this.counter],this.XMLData));
      this.counter++
      }
      }
      }

      evtDispatchExample.fla

      Inside this fla on very first frame I have following code

      import eventDispatch.*;
      var DEvt_obj:Evt=new DEvt();
      DEvt_obj.loadXML("xml.xml",'xml0 File');
      DEvt_obj.loadXML("xml1.xml",'xml1 File');
      DEvt_obj.loadXML("xml2.xml",'xml2 File');//----and so on---
      DEvt_obj.addEventListener(CustEvent.XMLLoaded,XMLL oaded);
      function XMLLoaded(evt:CustEvent) {
      //--- here we can check which XML file is loaded----
      if(evt.XMLRef=='xml0'){
      //--- do necessary task when this file loaded
      //---XML data can be found in evt.XMLData
      trace(evt.XMLData) //-- property of CustEvent class
      }
      if(evt.XMLRef=='xml1'){
      //--- do necessary task when this file loaded
      //---XML data can be found in evt.XMLData
      trace(evt.XMLData) //-- property of CustEvent class
      }
      //---- and so on for n number of XML file----
      }