Skip to content

Instantly share code, notes, and snippets.

@RKAN
Created July 21, 2013 06:35
Show Gist options
  • Save RKAN/6047721 to your computer and use it in GitHub Desktop.
Save RKAN/6047721 to your computer and use it in GitHub Desktop.
Create a Preloader class in Actionscript 3
/*
* 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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment