Hello! And thanks for your time!
Sorry for the "pseudo code", I wrote that very quickly, and I don't even realize that I left errors!
So yes, the idea is to create a kind of asset manager. A class that create and display a loading screen (with a progress bar), and behind load everything we need through XML and some methods I can call to get assets on my scene.
Right now, I repeat the loading stuff in every class (eg: level 1, 2, 3...). You can have a look of this in this video. First I have the "Main Menu" class, when I touch "Play", the main menu fade out, and the "Select World" class, first with the loading screen (which is inside this class) and when it's complete, display the scene: http://youtu.be/yokcjv69JtY?t=38s
package {
public class Level1 {
public function Level1() {
// Load XML --
xmlLoader = new XMLLoader ( "PathToTheXML", { onComplete:loadStuff } );
}
private function loadStuff( e:LoaderEvent ):void {
// ProgressBar --
progressBar = new Sprite();
addChild( progressBar );
// LoaderMax --
loaderMax = LoaderMax.getLoader( "loaderID" );
loaderMax.addEventListener( LoaderEvent.PROGRESS, loaderProgress );
loaderMax.addEventListener( LoaderEvent.COMPLETE, loaderComplete );
loaderMax.load();
}
private function loaderProgress( e:LoaderEvent ):void {
progressBar.scaleX = e.target.progress;
}
private function loaderComplete( e:LoaderEvent ):void {
// Clean screen --
removeChild( progressBar );
// Load stuff --
texture1 = TextureFromBitmapData( LoaderMax.getLoader( "contentID-1" ) );
texture2 = TextureFromBitmapData( LoaderMax.getLoader( "contentID-2" ) );
// Display object on the scene --
sprite1 = new Sprite( texture1 );
addChild( sprite1 );
}
}
Not very handy because if I made a change on the loader, I have to open every class, that's why I want to create a specitif "LoaderScreen". A screen that I can add on top of the scene I want to load, then when to everything is loaded I can call what I need ( images, textures, sounds... ).
So, the main application should looks like this:
So first, I create the LoaderScreen class:
package {
public function LoaderScreen() {
xmlLoader = new XMLLoader ( "PathToTheXML", { onComplete:loadStuff } );
}
private function loadStuff( e:LoaderEvent ):void {
// ProgressBar --
progressBar = new Sprite();
addChild( progressBar );
// LoaderMax --
loaderMax = LoaderMax.getLoader( "loaderID" );
loaderMax.addEventListener( LoaderEvent.PROGRESS, loaderProgress );
loaderMax.addEventListener( LoaderEvent.COMPLETE, loaderComplete );
loaderMax.load();
}
private function loaderProgress( e:LoaderEvent ):void {
progressBar.scaleX = e.target.progress;
}
private function loaderComplete( e:LoaderEvent:void {
dispatchEvent( new Event( "LoaderComplete", true, true ) );
}
// HELPERS --
// Call from everywhere to get asset
public function getAssetIMG( idAsset:String ):BitmapData {
// Return bitmapdata
return LoaderMax.getLoader( idAsset ).rawContent.bitmapData;
}
public function getAssetATF( idAsset:String ):ByteArray {
// Return bytearray
return LoaderMax.getContent( idAsset );
}
}
Now I can create scenes where I can call the LoaderScreen. The LoaderScreen will be on top of the scene. So it will display the progress bar, and when it completes, I can remove the LoaderScreen, to see the scene behind:
package {
public class Level1 {
public function Level1() {
// Create a LoaderScreen to on top of this scene
loaderScreen = new LoaderScreen();
guiLayer.addChild( loaderScreen );
// I create an EventListener, waiting for the "Complete Event" of the LoaderScreen
loaderScreen.addEventListener( "LoaderComplete", initScene );
}
private function initScene():void {
// Load stuff through the "getAsset" function --
texture1 = TextureFromBitmapData( loaderScreen.getAssetIMG( "ID1" ) );
texture2 = TextureFromBitmapData( loaderScreen.getAssetIMG( "ID2" ) );
texture3 = TextureFromATF( loaderScreen.getAssetATF( "ID3" ) );
// Display Object --
sprite1 = new Sprite( texture1 );
addChild( sprite1 );
// Init scene complete --
guiLayer.removeChild( loaderScreen );
// Now I can see the scene --
}
}
I hop it's a little bit more clean now. I have my own AssetManager, but I wanted to use GSAP to be more flexible ( can animate the loader / create a TimelineMax for fade In / Out the scene... etc... ). But right now, when I try to call object outside the function loaderProgress( e:LoaderEvent ), it returns an error / null. Thanks again for your time!