Return to Snippet

Revision: 61339
at December 6, 2012 16:42 by jquery404


Initial Code
/*
* main.fla -> main.as 
*/

package  
{
	import flash.display.MovieClip;
	import flash.events.Event;
	/**
	 * ...
	 * @author Faisal
	 */
	public class Main extends MovieClip
	{
		
		public function Main() 
		{
			
			if (stage)
			{
				init();		
			}
			else
			{
				addEventListener(Event.ADDED_TO_STAGE, init);	
			}			
			
		}
		
		
		private function init(e:Event = null):void 
		{
			trace("I am main file loaded from Main.fla :)");			
		}
		
	}

}



/*
* preloader.fla -> preloader.as
*/

package  
{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	/**
	 * ...
	 * @author Faisal
	 */
	public class Preloader extends MovieClip
	{
		
		public function Preloader() 
		{
			if (stage)
			{
				init();
			}
			else
			{
				addEventListener(Event.ADDED_TO_STAGE, init);
			}
			
		}
		
		private function init():void 
		{
			var url:URLRequest = new URLRequest("Main.swf");
			var myLoader:Loader = new Loader();
			myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderComplete);
			myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoaderProgress);
			
			myLoader.load(url);
		}
		
		private function onLoaderComplete(e:Event):void 
		{
			trace(e.currentTarget.content);
			var a:MovieClip = new MovieClip();
			stage.addChild(a);
			a.addChild(e.currentTarget.content)
		}
		
		private function onLoaderProgress(e:ProgressEvent):void 
		{
			var loaded:Number = (e.bytesLoaded / e.bytesTotal) * 100;
			trace(loaded);
		}
		
	}

}

Initial URL

                                

Initial Description
A complete example of how we can create a Preloader in actionscript3 to load an external .swf file.

STRUCTURE
------------------
Source Swf
|_ Main.fla
|_ Main.as
|_ Main.swf
Preloader Swf
|_

Initial Title
Create a Preloader class in Actionscript 3

Initial Tags
class

Initial Language
ActionScript 3