Loading an XML

private var xmlLoader:XMLLoader;
private var xml:XML;

private function loadXml():void
    xmlLoader = new XMLLoader('xmlPath', { onComplete:onXmlLoaded, onFail:onXmlFail, noCache:true } );

private function onXmlFail(e:LoaderEvent):void
    trace("ERROR LOADING XML" + e.text);

private function onXmlLoaded(e:LoaderEvent):void
    xml =  e.target.content;

Here is a list of the XMLoader properties I use the most:


If noCache is true, a “gsCacheBusterID” parameter will be appended to the url with a random set of numbers to prevent caching. Very useful in order to avoid the “please clear your browser cache” conversation with your clients!

xmlLoader = new XMLLoader('xmlPath', { noCache:true } );

Loading images

Loading a single image

private var imgLoader:ImageLoader;
private var preloaderText:Textfield; // your preloader textfield
private var imgPath:String = 'images/01.jpg';

private function loadSingleImage():void
    imgLoader = new ImageLoader( imgPath, {container:imgHolder, alpha:0, onProgress: onImgLoadProgress, onComplete: onImgLoadComplete, onFail: onImgLoadFail});

private function onImgLoadProgress(e:LoaderEvent):void
    preloaderText.text = e.target.progress * 100 + ' %';

private function onImgLoadComplete(e:LoaderEvent):void
    var loadedImage:ContentDisplay = e.target.content;
    TweenMax.to(loadedImage, 0.8, {alpha:1});

private function onImgLoadFail(e:LoaderEvent):void
    trace("ERROR LOADING XML" + e.text);

Loading a swf


Loading multiple files (xmls, images, flvs, etc.)

Get code hinting and strict data typing

If you don’t mind a little extra file size and slightly longer code, you can use the various data classes in the com.greensock.loading.data package to define the vars object that you pass into the constructor instead of a generic {} object. These data classes (like LoaderMaxVars, SWFLoaderVars, ImageLoaderVars, etc.) deliver some yummy code hinting so that you don’t have to refer to the docs so much to remember the various special properties that are available. And of course this technique also gives you strict data typing for improved debugging. For example:

//using a variable (long syntax)
var config:LoaderMaxVars = new LoaderMaxVars();
var queue:LoaderMax = new LoaderMax(config);


Post A Comment

Anti-Spam Quiz: