This tutorial will explain how you can use the BulkLoaderResourceProvider to dynamicly load the resources in your game in multiple phases.
See this tutorial in action
Download the sources of this tutorial
Download the flash + assets of this tutorial
1. The BulkLoaderResourceProvider Class
The BulkLoaderResourceProvider is subclassed from ResourceProviderBase.
The constructor takes 2 parameters and will auto-register with the ResourceManager.
name:String - this is the name of the BulkLoader object that is created for this BulkLoaderResourceProvider.
numConnections:int = 12 - number of connections the BulkLoader object will use.
The BulkLoaderResourceProvider has a phase property that tells you the current loading phase. The loading phase always starts with 1 and increments after all resources of that phase have been loaded and as long as the phase has resources to be loaded.
The BulkLoaderResourceProvider has a function load(onLoaded:Function = null, onProgress:Function = null, onProvideResources:Function = null):void that can be used to start loading the resources.
-
param onLoaded is a callback function that is called when all resources of the current phase have been loaded.
function onLoaded(phase:int):void -
param onProgress is a callback function that is called when resources of the current phase are being loaded. Progress is a percentage (0-100)
function onProgress(phase:int, progress:int):void -
param onProvideResources is a callback function that is called to get the bulkResource objects (Array) of the current loading phase. These bulkResource objects specify the resources that have to be loaded.
function onLoaded(phase:int):voidIf this parameter is ommited the function provideResources():void is called to get the current phase bulkResource objects.
The Array of bulkResource objects that must be the result of the onProvideResources callback function or the overridden provideResources function is a normal Array that holds objects that has 2 attributes:
-
url : String
url/source of the resource to be loaded -
type : Resource
type of Resource (ImageResource, XMLResource, MP3Resource etc )
var bulkResourceObjects:Array = [
{ url : "data/test1.xml", type : XMLResource },
{ url : "data/test2.xml", type : XMLResource },
{ url : "data/test3.xml", type : XMLResource }
];
The BulkLoaderResourceProvider has a function provideResources():Array that can be overridden in a subclass that will be called when requesting the current phase bulkResource objects.
2. Subclassing the BulkLoaderResourceProvider class
If one wants to create a seperate class that will do multi-phase resource loading just subclass the BulkResourceProvider class and override the function provideResources():Array
Multi-phase loading can be usefull when one would like to first load some resources (xml/images/sounds) in a first pohase and display a kind of splash-screen while all other resources (the big bulk) will be loaded in phase 2.
public class SampleResources extends BulkLoaderResourceProvider
{
public function SampleResources()
{
super("sampleLoader");
}
protected override function provideResources():Array
{
switch(phase)
{
case 1:
// return this hard-coded array of resource objects so it will
// be processed
return [
{ url : "data/test1.xml", type : XMLResource },
{ url : "data/test2.xml", type : XMLResource },
{ url : "data/test3.xml", type : XMLResource }
];
break;
case 2:
// create the array of resource objects programmaticly
var resObjects:Array = new Array();
for (var i:int=4; i<=6; i++)
{
// create a resource object
var resObject:Object = new Object();
resObject.url = "data/test"+i+".xml";
resObject.type = XMLResource;
// add the resource object to the array
resObjects.push(resObject);
}
// return the array so it will be processed
return resObjects;
break;
}
return null;
}
}
As you can see in the code above, we just extend the BulkLoaderResourceProvider. We call the super("sampleLoader") constructor so that our provider is registered with the ResourceManager and the bulkloader is prepared.
We have overridden the function provideResources():Array and use that to provide the array's with bulkResource objects for each (2) phase.
We start the loading in our main application like this :
// create subclass var sampleResources:SampleResources = new SampleResources(); // initiate the load sampleResources.load(resourcesLoaded , resourcesProgress);
The resourceLoaded and resourcesProgress function are used to display some loading information on screen.
4. Using the BulkLoaderResourceProvider class to do a phased load
We can do a multi-phased resource loading using the BulkLoaderResourceProvider using the load function and providing a onProvideResources callback function.
We would start the loading like this:
var bulkResourceProvider:BulkLoaderResourceProvider = new BulkLoaderResourceProvider("sampleLoader");
// initiate the load - provide the provideData call back function
bulkResourceProvider.load(resourcesLoaded , resourcesProgress, provideResources);
Where the callback function provideResources(phase:int):Array would look like
private function provideResources(phase:int):Array
{
switch(phase)
{
case 1:
// return this hard-coded array of resource objects so it will
// be processed
return [
{ url : "data/test1.xml", type : XMLResource },
{ url : "data/test2.xml", type : XMLResource },
{ url : "data/test3.xml", type : XMLResource }
];
break;
case 2:
// create the array of resource objects programmaticly
var resObjects:Array = new Array();
for (var i:int=4; i<=6; i++)
{
// create a resource object
var resObject:Object = new Object();
resObject.url = "data/test"+i+".xml";
resObject.type = XMLResource;
// add the resource object to the array
resObjects.push(resObject);
}
// return the array so it will be processed
return resObjects;
break;
}
return null;
}
The callback function provideResources(phase:int):Array and subclassed function provideResources():Array are almost the same except the phase parameter of the callback. This because phase is a known property of the subclass.
See this tutorial in action
Download the sources of this tutorial
Download the flash + assets of this tutorial