AS3 Using Custom Event – dispatchEvent

Sometimes, we want a particular class to do something however, that task requires time to complete (e.g. loading XML or list of images remotely) and some process still awaits a form of “signal” to proceed. Pretty much like the real life shown in picture below.

See sample demo below. It’s a simple rss Reader that reads feed from CNN. Today’s sample is to show how a class can dispatch a custom event so that, the main class (which is listening to the customEvent) can trigger a specific function.

See source code below on how Main.as which listens to a customEvent called RSSReady and triggers a function when mcRSSContainer is done with loaded the data.

Main.as
package  
{
	import flash.display.MovieClip;
	/********************************************************************
	 * Description : This is the main document root file.
	 * @author Alex T.
	 ********************************************************************/
	public class Main extends MovieClip
	{
		public var mcPreloader : MovieClip;
		public var mcRSSContainer:MovieClip;
 
		public function Main() 
		{
			trace("init!") ;
			mcPreloader.visible = true;
			// Since, mcRSSContainer is the stage object of the class, 
			// we attach the listener here.This behaviour is similar to a doctor 
			// placing a stethoscope on your body to listen for heartbeat.
			// listening for the RSSReady customEvent to be dispatch.
			mcRSSContainer.addEventListener("RSSReady", handleRSSReady);
		}
 
		private function handleRSSReady(event):void {
			mcPreloader.visible = false;
		}
	}
}
mcRSSContainer.as
package com.isgoodstuff 
{
	import fl.controls.List;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.net.FileReferenceList;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	/********************************************************************
	* Description : This is mcRSSContainer Class. 
	* Contains a ListBox component. This class will load RSS using 
	* a URLLoader and then once loaded, sends a customEvent  or 
	* (dispatchEvent) which the Main is listening to, in order to hide 
	* mcPreloader.
	* @author Alex T.
	********************************************************************/
	public class mcRSSContainer extends MovieClip
	{
		public var listBox:List;
		public var rssLoader:URLLoader = new URLLoader();
		public var rssXML:XML = new XML();
		public function mcRSSContainer() 
		{
			trace("mcRSSContainer successfully initiatialize");
			startLoadingRSS();
		}
 
		private function startLoadingRSS():void {
			var rssURL:URLRequest =  new URLRequest("http://rss.cnn.com/rss/edition.rss");
			rssLoader.addEventListener(Event.COMPLETE, rssLoaded);
			rssLoader.load(rssURL);
		}
 
		private function rssLoaded(event):void {
			rssXML.ignoreWhitespace = true;
			rssXML = XML(rssLoader.data);
			for(var item:String in rssXML.channel.item) {
				listBox.addItem( {label: rssXML.channel.item[item].title } );
			}
			// This means the class has done all the RSS loading. Now we need to send a custom
			// Event (dispatchEvent) like a shout to other classes listening that data is ready.
			// The string RSSReady can be any other custom Event name you wanted. Just
			// Make sure when attaching listener has to "listen For" the particular string event.
			// Now see the mcRSSContainer.addEventListener("RSSReady", handleRSSReady); 
			// under Main.as
			dispatchEvent( new Event("RSSReady") );
		}
	}
 
}

For today’s tutorial click the link below.

Download Here