Pixlib - Tut 03 - How to load assets at runtime
readed 4742 timesHello there.
As said in the title this tutorial will help you to load assets at runtime. At the same time we will see how to use FlashDevelop and MTASC to create a swf. So no need to open flash anymore...
Ok first, if you didn't please look at the first tutorial which give you the basis of the pixlib framework.
This tutorial will use the source files of the first tutorial as a starting point. You can download those files from here
First Tutorial Sources
Download the first tutorial sources (Size: 49.8 KB)
The sources of this tutorial
Tutorial Sources
Download this tutorial's sources (Size: 162.6 KB)
I. Refreshing our memory
So remember, we had a fla importing the Application.as class and then creating an instance of it to start the whole application.
-
import net.webbymx.projects.tutorial01.Application;
-
var apz : Application = new Application( this );
The fla had the assets in it's library so we were doing in the Application.as
-
// Instantiating the views
-
var digital : MovieClip = this.attachMovie("mc_digital", "mc_digital", 0 );
-
var vDigital : ViewDigital = new ViewDigital ( digital );
to instantiate a view.
II. SWFMILL and MTASC
Now, we don't want to use flash anymore ( anyway for the development side ).
We will use 2 tools:
- SWFMILL to create swfs ( empty or not, see the project page for more info please )
- MTASC to compile our classes with and push them in the swf created by SWFMILL
If you don't know how to use or integrate those great tools to your editor, go on their respective project page and / or on the project page of your editor.
For my case, I'm using FlashDevelop a great FREE editor ( only for window user sorry :/ ). It has MTASC and SWFMILL in it, and it's as simple as clicking a button to compile your project.
III. Show me the sh*t!!!
Ok, ok...
a. Pixlib Classes used
We will use 3 classes that are part of the Pixlib framework.
- The first one is the ConfigLoader, which helps you loading the XML setting up your application
- The second one is the GraphicLib, which helps you loading an asset in your application.
- The third one is the LibStack, which is a download manager
Why that?
First we need to load an xml to know where our assets are.
Then we will use the LibStack, push in it all the GraphicLib created ( one for each asset ) and start the download process.
By the way if you dunno how works the ConfigLoader class have a look here please.
b. A typical Application.as
Here is the code of a typical Application.as class. I'm now always using this as a template for all my projects ( until further improvement as usual
)
It's a bit long, but it's just for you to have a quick look. We will go anyway in details in the code...
-
// Debug
-
import com.bourre.log.Logger;
-
import com.bourre.log.LogLevel;
-
import com.bourre.log.PixlibDebug;
-
import com.bourre.utils.LuminicTracer;
-
-
// Models
-
import com.bourre.core.Model;
-
import net.webbymx.projects.tutorial03.models.*;
-
-
// Views
-
import com.bourre.visual.MovieClipHelper;
-
import net.webbymx.projects.tutorial03.views.*;
-
-
// Controller
-
import net.webbymx.projects.tutorial03.commands.Controller;
-
-
// Personnal EventBroadcaster
-
import net.webbymx.projects.tutorial03.events.XEventBroadcaster;
-
-
// Events
-
import com.bourre.events.BasicEvent;
-
import com.bourre.events.IEvent;
-
import net.webbymx.projects.tutorial03.events.EventList;
-
-
// ConfigLoader
-
import com.bourre.data.libs.ConfigLoader;
-
import com.bourre.data.libs.XMLToObjectDeserializer;
-
-
// Libstact, GraphicLib
-
import com.bourre.data.libs.LibStack;
-
import com.bourre.data.libs.GraphicLib;
-
import com.bourre.data.libs.GraphicLibEvent;
-
-
// tweening
-
import com.mosesSupposes.fuse.*;
-
-
class net.webbymx.projects.tutorial03.Application
-
extends MovieClip {
-
-
/* ****************************************************************************
-
* PRIVATE STATIC VAR
-
**************************************************************************** */
-
private static var _oI : Application;
-
-
-
/* ****************************************************************************
-
* PRIVATE VARIABLES
-
**************************************************************************** */
-
// VAR
-
private var _oConf : Object;
-
-
-
/* ****************************************************************************
-
* CONSTRUCTOR
-
**************************************************************************** */
-
private function Application(container) {
-
// the movieclip is transtyped as a application object
-
container.__proto__ = this.__proto__;
-
container.__constructor__ = Application;
-
this = container;
-
_oI = this;
-
-
// init the object
-
_init();
-
-
// load the conf
-
_loadConf();
-
}
-
-
public static function getInstance() : Application {
-
if ( _oI == undefined ) {
-
if ( arguments[ 0 ] == undefined || typeof( arguments[ 0 ] ) != "movieclip" ) PixlibDebug.FATAL( "Cannot create the Application object. You need to pass a MovieClip as a parameter" );
-
else var apz : Application = new Application( arguments[ 0 ] );
-
}
-
return _oI;
-
}
-
-
public static function main( mc : MovieClip ) : Void {
-
var apz : Application = Application.getInstance( mc );
-
}
-
-
-
/* ****************************************************************************
-
* PRIVATE FUNCTIONS
-
**************************************************************************** */
-
/**
-
* Init the Application
-
* @param Void
-
*/
-
private function _init() : Void {
-
Logger.LOG( "Application :: _init" );
-
-
Stage.align = "TL";
-
-
// register the ZigoEngine
-
ZigoEngine.register( PennerEasing, FuseItem, FuseFMP );
-
ZigoEngine.EASING = "linear";
-
-
// init the debugger
-
Logger.getInstance().addLogListener( LuminicTracer.getInstance() );
-
-
// create the model
-
var mClock : ModelClock = new ModelClock();
-
-
// init the controller with our custom EventBroadcaster class
-
Controller.getInstance( XEventBroadcaster.getInstance() );
-
}
-
-
-
/**
-
* Load the congif.xml file
-
* @param Void
-
*/
-
private function _loadConf( Void ) : Void {
-
Logger.LOG( "Application :: _loadConf" );
-
-
// Define some properties of the deserializer
-
XMLToObjectDeserializer.DESERIALIZE_ATTRIBUTES = true;
-
XMLToObjectDeserializer.PUSHINARRAY_IDENTICAL_NODE_NAMES = true;
-
-
// creating the object holding the result for a map
-
_oConf = new Object();
-
-
// creating the config loader
-
var _cfLoader : ConfigLoader = new ConfigLoader( _oConf );
-
// define the callback once the xml is loaded
-
_cfLoader.addEventListener( ConfigLoader.onLoadInitEVENT, this, loadLoader );
-
// load by default the "config.xml" file
-
_cfLoader.load();
-
}
-
-
-
public function _loadAssets () : Void {
-
Logger.LOG( "Application :: loadAssets" );
-
-
// total bytes loaded
-
var totalBytes : Number = 0;
-
-
// Loading everything
-
var libContainer : MovieClip = this["__libContainer"];
-
var libs : LibStack = new LibStack();
-
-
// get the array of assets to load
-
var aAssets : Array = _oConf.assets.asset;
-
for( var i = 0; i <aAssets.length; ++i ) {
-
// create the graphic lib
-
var gl : GraphicLib = new GraphicLib( libContainer, aAssets[ i ].depth, false );
-
// define callbacks
-
gl.addEventListener( GraphicLib.onLoadInitEVENT, this, onAssetLoaded );
-
gl.addEventListener( GraphicLib.onLoadProgressEVENT, this, onAssetProgress );
-
// add to the LibStack
-
libs.enqueue( gl, aAssets[ i ].view, _oConf.paths.assets + aAssets[ i ].url );
-
// update the totalBytes
-
totalBytes += aAssets[ i ].size;
-
}
-
-
// set the total bytes
-
var vLoader : ViewPreloader = ViewPreloader( MovieClipHelper.getMovieClipHelper( ViewList.VIEW_PRELOADER ) );
-
vLoader.setTotalBytes( totalBytes );
-
-
// define the call back events
-
libs.addEventListener ( LibStack.onLoadCompleteEVENT, this );
-
libs.addEventListener( LibStack.onTimeOutEVENT, this );
-
-
// Start the download
-
libs.execute();
-
}
-
-
/**
-
* Build the Application
-
* @param Void
-
*/
-
private function _build( Void ) : Void {
-
Logger.LOG( "Application :: _build" );
-
-
// you can hide the preloader from inside a command ( like in a command StartApplication )
-
var vLoader : ViewPreloader = ViewPreloader( MovieClipHelper.getMovieClipHelper( ViewList.VIEW_PRELOADER ) );
-
vLoader.fadeout();
-
-
// create views
-
var vDigital : ViewDigital = new ViewDigital ( ViewList.VIEW_DIGITAL );
-
var vAnalog : ViewAnalog = new ViewAnalog ( ViewList.VIEW_ANALOG );
-
var vTools : ViewTools = new ViewTools ( ViewList.VIEW_TOOLS );
-
-
// views listening to the model
-
var mClock : ModelClock = ModelClock( Model.getModel( ModelList.MODEL_CLOCK ) );
-
mClock.addListener( vDigital );
-
mClock.addListener( vAnalog );
-
mClock.addListener( vTools );
-
-
// The clock start playing
-
XEventBroadcaster.getInstance().broadcastEvent( new BasicEvent( EventList.START_CLOCK ) );
-
}
-
-
-
/* ****************************************************************************
-
* PUBLIC FUNCTIONS - CALLBACKS
-
**************************************************************************** */
-
/**
-
* load the loader ( funny :/ )
-
* @param e
-
*/
-
public function loadLoader( e : IEvent ) : Void {
-
Logger.LOG( "Application :: loadAssets" );
-
// this movieclip will be the main container for all the assets loaded
-
var libContainer = createEmptyMovieClip( "__libContainer" , this.getNextHighestDepth() );
-
-
// Creating the GraphicLib for the prelaoder
-
var gl : GraphicLib = new GraphicLib( libContainer, _oConf.assets.preloader.depth, false );
-
// define the callbacks
-
gl.addEventListener( GraphicLib.onLoadInitEVENT, this, onLoaderLoaded );
-
/* define the name of the library - the same as the view name
-
* thus when we are creating the view, the view will look in all the GraphicLib will find one
-
* with the same name and will use it */
-
gl.setName( _oConf.assets.preloader.view );
-
// load the preloader
-
gl.load( _oConf.paths.assets + _oConf.assets.preloader.url );
-
-
}
-
-
/**
-
* call back once the graphic of the laoder is called
-
* @param e
-
*/
-
public function onLoaderLoaded( e : GraphicLibEvent ) : Void {
-
Logger.LOG( "Application :: onLoaderLoaded" );
-
// create the view, will link automatically to the graphic
-
var vLoader : ViewPreloader = new ViewPreloader( e.getName() );
-
vLoader.show();
-
-
// start the download process
-
_loadAssets();
-
}
-
-
-
/**
-
* Update the loader view
-
* @param e
-
*/
-
public function onAssetProgress( e : GraphicLibEvent ) : Void {
-
var vLoader : ViewPreloader = ViewPreloader( MovieClipHelper.getMovieClipHelper( ViewList.VIEW_PRELOADER ) );
-
vLoader.onLoadProgress( e.getLib().getBytesLoaded() );
-
}
-
-
-
/**
-
* Update the loader view
-
* @param e
-
*/
-
public function onAssetLoaded ( e : GraphicLibEvent ) : Void {
-
Logger.LOG( "Application :: onAssetLoaded : " + e.getName() );
-
// e.getView().stop();
-
var vLoader : ViewPreloader = ViewPreloader( MovieClipHelper.getMovieClipHelper( ViewList.VIEW_PRELOADER ) );
-
vLoader.addBytes( e.getLib().getBytesTotal() );
-
}
-
-
/**
-
* Once everything is loaded
-
*/
-
public function onLoadComplete() : Void {
-
Logger.LOG( "Application :: onLoadComplete" );
-
_build();
-
}
-
-
/* ****************************************************************************
-
* GETTER & SETTER
-
**************************************************************************** */
-
}
Ok let's see the code in details now.
As usual my Application.as when created is calling the _init() function, which is setting up ( here ) the fuse tween engine, the Looger, and controller using our custom EventBroadcaster ( why using a custom EventBroadcaster is explain in the first tutorial )
LOADING THE CONFIGURATION
Then following the process said above, I'm first loading the config.xml file
-
/**
-
* Load the congif.xml file
-
* @param Void
-
*/
-
private function _loadConf( Void ) : Void {
-
Logger.LOG( "Application :: _loadConf" );
-
-
// Define some properties of the deserializer
-
XMLToObjectDeserializer.DESERIALIZE_ATTRIBUTES = true;
-
XMLToObjectDeserializer.PUSHINARRAY_IDENTICAL_NODE_NAMES = true;
-
-
// creating the object holding the result for a map
-
_oConf = new Object();
-
-
// creating the config loader
-
var _cfLoader : ConfigLoader = new ConfigLoader( _oConf );
-
// define the callback once the xml is loaded
-
_cfLoader.addEventListener( ConfigLoader.onLoadInitEVENT, this, loadLoader );
-
// load by default the "config.xml" file
-
_cfLoader.load();
-
}
You should know this already, otherwise go look the second tutorial
LOADING THE LOADER
So when our config.xml is loaded the function loadLoader is called
-
/**
-
* load the loader ( funny :/ )
-
* @param e
-
*/
-
public function loadLoader( e : IEvent ) : Void {
-
Logger.LOG( "Application :: loadAssets" );
-
// this movieclip will be the main container for all the assets loaded
-
var libContainer = createEmptyMovieClip( "__libContainer" , this.getNextHighestDepth() );
-
-
// Creating the GraphicLib for the prelaoder
-
var gl : GraphicLib = new GraphicLib( libContainer, _oConf.assets.preloader.depth, false );
-
// define the callbacks
-
gl.addEventListener( GraphicLib.