Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mikatalk
Created November 6, 2015 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikatalk/045ef797f33c85df4ad2 to your computer and use it in GitHub Desktop.
Save mikatalk/045ef797f33c85df4ad2 to your computer and use it in GitHub Desktop.
Asset Loader Singleton
package com.samere
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
public class Skin extends EventDispatcher
{
private static var instance:Skin;
private var loader:Loader;
private var applicationDomain:ApplicationDomain;
public function Skin(pvt:Enforcer)
{
loader = new Loader();
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, function(event:ProgressEvent):void{
dispatchEvent(event);
});
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(event:Event):void{
applicationDomain = loader.content.loaderInfo.applicationDomain;
dispatchEvent(event);
});
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function(event:IOErrorEvent):void{
dispatchEvent(event);
});
}
public static function getInstance():Skin
{
if ( instance === null ) instance = new Skin(new Enforcer);
return instance;
}
public static function load(theme:String, systemPath:String):void
{
if ( instance === null ) instance = new Skin(new Enforcer);
instance.loader.load(new URLRequest(systemPath+theme+".swf"));
}
public static function getSprite(ref:String):Sprite
{
trace("[Skin] ~", ref);
return new (getInstance().applicationDomain.getDefinition(ref) as Class);
}
}
}
class Enforcer {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment