Resource Manager

Updated On: April 4, 2013

Few games can run without requiring extra data like images, levels, or sound. The ResourceManager lets you access files embedded in your SWF as well as external files. It also lets you indicate what kind of data is in each resource, so that once loading completes, the image or XML or sound (or whatever) is ready to go.

Loading Resources

// Example of loading an image:
ResourceManager.instance.load("../Assets/Image/testImage.jpg", ImageResource, _OnImageLoaded, _OnImageFailed);

// Callback when the load succeeds.
function _OnImageLoaded(resource:ImageResource):void
   // Store the loaded resource or do other activity.
   Image = resource;

// Callback when the load fails.
function _OnImageFailed(resource:ImageResource):void
   // Report the error.
   Logger.printError(this, "_OnImageFailed", "Couldn't load the image!");

Loading a resource manually is simple. You specify the filename, resource type, and callbacks for when the load completes or fails.

Resource types are subclasses of com.pblabs.engine.resource.Resource. Commonly used subclasses are XMLResource, ImageResource, and MP3Resource. Typically, these classes process the resource in some way and present you with finished data, such as a BitmapData in the case of the ImageResource.

Embedding Resources

public class MyResources extends ResourceBundle
// Images and sounds can be embedded directly.
   public var resmyimage:Class;

   public var resmyjpg:Class;

   [Embed(source = "../assets/sounds/mysound.mp3")]
   public var resmysound:Class;

   // Note that we are specifying mimeType for xml so it is embedded properly.
   [Embed(source = "../assets/levelDescriptions.xml", mimeType = 'application/octet-stream')]
   public var resmyleveldescriptions:Class;

   [Embed(source="../assets/levels/mylevel.pbelevel", mimeType='application/octet-stream')]
   public var resmylevel:Class;


Loading files externally is a bad idea. It makes it harder to distribute your game, and the extra round trips to the server can increase loading time significantly. In addition, resources embedded in the SWF can have additional compression put on them.

The easiest way to embed a resource in PBE is with the ResourceBundle class. Create a subclass of the ResourceBundle class, where each file to be included in the swf is given an [Embed] tag. Here are some examples of how to embed your resources in a ResourceBundle.

For most file types, this is all you have to do for them to show up and work properly. However, for XML and pbelevel files, we must include the mimeType='application/octet-stream' in the [Embed()] tag. Otherwise the compiler will fail to include them in the right format. If you have a SWF file, omitting the mimeType will cause mxmlc to re-encode the SWF, stripping any ActionScript it contains. Images without mimeType tags may be re-compressed, so be careful if download size is important.

new MyResources();

Instantiate MyResources, and it will register all of the resources with the ResourceManager according to their proper type. Usually you will want to do this immediately after you call PBE.startup.

Adding New Resource Types

The com.pblabs.engine.resource.Resource class is subclassed to add support for different resource types. This is very straightforward - just check out the XMLResource to see how it's done. You will want to override _onContentReady and provide accessors to get at whatever data is needed.

Compiler Meta Data

It is VERY important that you remember to include the following line in the additional compiler arguments so that resources can be embedded properly:


Follow Us For Updates