TextureManager class AS3

It has come to my attention that many people might not store their images in a BitmapData. However, for the people that like to dynamically load their images, I made this a while back and use it regularly. It’s a stripped down version of the one in my game engine:

Clicky

Just a simple FIFO image loader that loads one image after another.

Texture.as

package {
    import flash.display.BitmapData;
    public class Texture {
        public var loaded:Boolean;
        public var ID:String;
        public var bitmap:BitmapData;
        public function Texture(ID_:String) {
            loaded = false;
            ID = ID_;
        }
    }
}

TextureManager.as

package {
    import flash.display.*;
    import flash.events.*;
    import flash.net.URLRequest;
    public class TextureManager {
        //All the textures.
        public var textures:Array;
        //Array for textures waiting to be loaded.
        public var textureLoadQueue:Array;
        public var readyToLoadImage:Boolean;
        public var URLpath:String;
        public var textureTemp:Texture;
        public function TextureManager(URLpath_:String) {
            URLpath = URLpath_;
            textures = new Array();
            textureLoadQueue = new Array();
            readyToLoadImage = true;
        }
        public function Load(imageID_:String) {
            //Allocate a spot to identify if the texture is loaded
            textures[imageID_] = new Texture(imageID_);
            //Add to the load queue to download the image file
            textureLoadQueue.push(textures[imageID_]);
        }
        public function LoadQueue():void {
            if (textureLoadQueue.length>0) {
                if (readyToLoadImage) {
                    textureTemp = textureLoadQueue.shift();
                    trace("Loading: "+textureTemp.ID);
                    readyToLoadImage = false;
                    var loader:Loader = new Loader();
                    var urlReq:URLRequest = new URLRequest(URLpath+textureTemp.ID+".png");
                    loader.contentLoaderInfo.addEventListener(Event.INIT,completedLoading);
                    loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorLoading);
                    loader.load(urlReq);
                } else {
                    trace("Texture Loading...");
                }
            }
        }
        public function completedLoading(eventObj:Object){
            trace("Done Loading: " + textureTemp.ID);
            var loader:Loader = eventObj.target.loader;
            var info:LoaderInfo = loader.contentLoaderInfo;
            textureTemp.bitmap = loader.content.bitmapData;
            textureTemp.loaded = true;
            readyToLoadImage = true;
        }
        public function errorLoading(eventObj:Object){
            trace("Error Loading Image: " + eventObj);
            //An error occured. By default the next image will load: NOTE: if needed you may want to uncomment the lines that follow given what you want to do:
            //textureLoadQueue.unshift(textureTemp);//This will try to load the image again.
            //textureLoadQueue.push(textureTemp);//This will try to load the image again after the rest of the images.
            readyToLoadImage = true;
        }
        //Does not check if the image is actually loaded or exists, just returns the texture if found
        public function GetTexture(ID_:String):Texture {
            return textures[ID_];
        }
    }
}

Then to use it in your main class simply put:
public var tm:TextureManager = new TextureManager("");
“” means no directory will be used. Use “folderName/” for accessing a folder where the images are kept along with your swf.

To load an image you simply use Load(“imageName”);
Notice how there is no .png, this is because the png is added in the texture manager class. Since the only format I use png this simplified things. If you use more than one file format then you’ll have to change this in the TextureManager.as file. Ex: tm.Load(“particle1”);

Then on enter frame the function LoadQueue must be called. Ex: tm.LoadQueue();
This will go through and load all of the images one at a time.

Quick example I made a few weeks ago that uses this:
Clicky
(Click around. Each click makes 500 particles so don’t get too excited. Hold space to turn off rendering. Also the simulation is time modeled so the fps doesn’t matter it will run at the same speed ideally no matter the fps).