/ Published in: ActionScript 3
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
|_
STRUCTURE
------------------
Source Swf
|_ Main.fla
|_ Main.as
|_ Main.swf
Preloader Swf
|_
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/* * 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); } } }