Say i have 400 objects that contain the same loaded png, whats the
suggested way to avoid loading the same asset repeatedly?

Here's what I did, don't know if it's nuts though.
To avoid loading the same png every time I made a singleton of the
PhotoFrame class and return a copy of the origional png as a bitmap.
It works, don't know the performace difference between using a new
Loader vs creating cloned bitmaps each iteration.
What is the suggested way to avoid loading the same asset multiple
times?


for (var i:int=0; i < 400; i++){
     new Photo(photoXML, PhotoFrame.getInstance().frame);
}

public class PhotoFrame extends Sprite
{
     private static var instance:PhotoFrame = null;
     private var _loader:Loader = null;

     public function PhotoFrame(){
         _loader = new Loader();
         _loader.contentLoaderInfo.addEventListener(Event.COMPLETE,
onFrameLoaded);
        
_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,
onLoadError);
         _loader.load( new URLRequest("frame.png") );
     }

     public static function getInstance():PhotoFrame{
         if(instance == null) instance = new PhotoFrame();
         return instance as PhotoFrame;
     }

     public function get frame():Bitmap{
         var image:Bitmap = Bitmap(_loader.content);
         return new Bitmap(image.bitmapData.clone());
     }

     private function onLoadError(e:IOErrorEvent):void{
         trace(e.text);
     }

     private function onFrameLoaded(e:Event):void{
         trace('loaded');
     }
}


public class Photo extends EventDispatcher
{
     private var _frame:PhotoFrame;

     public function Photo(photoXML:XML, frame:PhotoFrame){
         _frame = frame;
         addChild(_frame);
         //...
     }

}



Reply via email to