The following two packages will allow you to utilize loading-after-the-fact (external loading of spritesheet png files) once the game/starling framework has started.
Please send any feedback or updates as I haven't added some necessary features (such as garbage collection once a scene has been switched). Also, there could be an easier way to do this but I haven't found it yet.
 //useage for starting the loading //"Africa" refers to the 'scene' or set of spritesheets to load (see the assets class) //the function name passed is a function (in the calling class) to be called when the entire set of spritesheets has loaded. Assets.loadAssets("Africa", loadingCompleteStartGame);  function loadingCompleteStartGame():void{ trace("loading scene complete!");  //start game here!!! }
package { import starling.textures.Texture; import flash.utils.Dictionary; import flash.display.Bitmap; import starling.textures.TextureAtlas; Â public class Assets { Â //the embedded sprite sheet used for anything we need at runtime (before loading the others - like a menu) [Embed(source="images/sprite-sheet-embedded.png")] public static const Sprite_sheet_embedded:Class; [Embed(source="images/sprite-sheet-embedded.xml", mimeType="application/octet-stream")] public static const Sprite_sheet_embedded_xml:Class; Â private static var gameTextures:Dictionary = new Dictionary(); private static var gameTextureAtlas:Dictionary = new Dictionary(); private static var counter:int = 0; private static var completedCall:Function; Â public static function getAtlas(name:String = "Sprite_sheet_embedded"):TextureAtlas { if(gameTextureAtlas[name] == undefined && name == "Sprite_sheet_embedded"){ trace("LOADING MISSING INDEX IN GAMETEXTUREATLAS[NAME] " + name); var texture:Texture = getTexture(name); var xml:XML = XML(new Assets[name + "_xml"]()); gameTextureAtlas[name] = new TextureAtlas(texture, xml); } Â return gameTextureAtlas[name]; } Â public static function getTexture(name:String):Texture { if(gameTextures[name] == undefined){ var bitmap:Bitmap = new Assets[name](); gameTextures[name] = Texture.fromBitmap(bitmap); } Â return gameTextures[name]; } Â public static function loadAssets(Set:String, Func:Function):void{ Â completedCall = Func; Â //for loading in multiple 'scenes' in a game if(Set == "Africa"){ Â //load all the africa spritesheets var africa1:AssetsLoader = new AssetsLoader("Africa_1", "africa-1", completedFunction); africa1.start(); var africa2:AssetsLoader = new AssetsLoader("Africa_2", "africa-2", completedFunction); africa2.start(); Â } else { //load an other sheet trace("NO OTHER SPRITE SHEETS TO LOAD!"); } } Â private static function completedFunction(name:String, xml:XML, bitmap:Bitmap):void { counter++; Â //add the xml and bitmap to the dictionary object as needed var texture:Texture = Texture.fromBitmap(bitmap); gameTextureAtlas[name] = new TextureAtlas(texture, xml); Â if(counter >= 2){ trace("completed loading ALL asset images/xml"); Â completedCall(); } } } }
And the asset loader class the above references
package { import flash.display.Loader; import flash.events.Event; import flash.events.IOErrorEvent; import flash.events.UncaughtErrorEvent; import flash.events.ProgressEvent; import flash.net.URLLoader; import flash.net.URLRequest; import flash.display.Bitmap; import flash.events.ErrorEvent; Â public class AssetsLoader { private var url:String = ""; private var id:String = ""; private var loader:Loader; private var completeFunc:Function; private var completeCounter:int = 0; private var xml:XML; private var loadedBitmap:Bitmap; Â public function AssetsLoader(Name:String, URl:String, Func:Function):void { loader = new Loader(); url = URl; id = Name; completeFunc = Func; } Â public function start():void { // create the loader for the png // when texture is loaded loader.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, imageUncaughtErrors); loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, imageProgressEvents); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageComplete); Â // load the texture loader.load(new URLRequest("http://google.com/images/sprites/" + url + ".png")); Â // create the loader for the xml file paired with the sprites var xmlloader:URLLoader = new URLLoader(); Â xmlloader.addEventListener(Event.COMPLETE, xmlComplete); xmlloader.addEventListener(IOErrorEvent.IO_ERROR, xmlErrorEvent); xmlloader.load(new URLRequest("http://google.com/images/sprites/" + url + ".xml")); } Â private function xmlComplete(event:Event):void { trace(id + " xml load complete"); xml = new XML(event.target.data); Â checkLoadingComplete(); } Â private function xmlErrorEvent(event:IOErrorEvent):void { trace(id + " error loading xml file"); } Â private function imageComplete(event:Event):void { trace(id + " image load complete"); Â // use the loaded image and add it to the dictionary loadedBitmap = event.currentTarget.loader.content as Bitmap; //gameTextures[id] = loadedBitmap; Â checkLoadingComplete(); } Â private function imageUncaughtErrors(event:UncaughtErrorEvent):void { //we'll need to try again possibly or tell the user of the complete and utter failure! trace(id + " things went completely wrong!"); if (event.error is Error) { var error:Error = event.error as Error; // do something with the error trace(id + " error message: " + error.message); } else if (event.error is ErrorEvent) { var errorEvent:ErrorEvent = event.error as ErrorEvent; // do something with the error trace(id + " error event"); } else { // a non-Error, non-ErrorEvent type was thrown and uncaught trace(id + " unknown or other error, we really should stop and access our life choices"); } } Â private function imageProgressEvents(event:ProgressEvent):void { var prog:int = Math.ceil(event.bytesLoaded / event.bytesTotal * 100); Â trace(id + " progress: " + prog + "%"); } Â private function checkLoadingComplete():void { completeCounter++; Â if(completeCounter >= 2){ trace(id + " completed"); //call a custom function when loading completes! completeFunc(id, xml, loadedBitmap); } } } }
POSTED 7 MONTHS AGOÂ #












