Created
January 21, 2011 00:49
-
-
Save davidortinau/789047 to your computer and use it in GitHub Desktop.
LoaderMax Lazy Loading Strategy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// What I usually do is have an XML manifest that lists all of my assets as loaders for LoaderMax to handle | |
// For example: | |
<?xml version="1.0"?> | |
<config idletime="90000" launchPosition="right" webServiceURL="" scaleFactor="1"> | |
<XMLLoader name="en" url="/assets/xml/content_en.xml" load="true" /> | |
<XMLLoader name="es" url="/assets/xml/content_es.xml" load="false" /> | |
<XMLLoader name="zh" url="/assets/xml/content_cn.xml" load="false" /> | |
</config> | |
// To LAZY Load the first thing is to specify load="false". LoaderMax will retain a | |
// reference to the loader, in this case an XMLLoader, but will not immediately load the content | |
// I "think" each of my xml files in the above example could have their own loaders specified and | |
// LoaderMax will act on those also. I'm not doing that in this case though. | |
// My config service loads up like this | |
public class ConfigService | |
{ | |
public var complete:Signal; | |
public function ConfigService() | |
{ | |
complete = new Signal(); | |
} | |
public function load():void | |
{ | |
LoaderMax.activate([XMLLoader]); | |
var loader:XMLLoader = new XMLLoader("assets/xml/config.xml", {name:"config", onComplete:onReady}); | |
loader.load(); | |
} | |
private function onReady(e:LoaderEvent) : void { | |
var model:AppModel = AppModel.getInstance(); | |
var xml:XML = LoaderMax.getLoader("config").content as XML; | |
model.timeout = int(xml.@idletime); | |
model.launchScreenPosition = xml.@launchPosition; | |
model.webServiceURL = xml.@webServiceURL; | |
model.scaleFactor = Number(xml.@scaleFactor); | |
complete.dispatch(); | |
} | |
} | |
// when the user switches languages and I need to load in the assets for that language, so I just call | |
// load on them | |
var xmlLoader:XMLLoader = LoaderMax.getLoader("zh") as XMLLoader; | |
xmlLoader.addEventListener(LoaderEvent.COMPLETE, onComplete); | |
xmlLoader.load(); | |
// and after it's loaded, when the asset is needed | |
var staticMap:Sprite = Sprite( LoaderMax.getContent("shortMap") ); | |
// I don't believe there's any penalty to calling load on a loader already loaded. ;) | |
// check out http://www.greensock.com/loadermax-tips/ | |
// If I was using this strategy widely in the app, I'd have an AssetFactory class responsible for managing | |
// all the loader mumbo jumbo, even though LoaderMax can be used as a singleton |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment