I’ve noticed that there’s some stuff (lines of code basically) that I always reuse when working on projects, for example when loading bitmaps.
So I decided to create a class called BitmapLoader:
package as3.bitmap
{
import flash.display.Sprite;
import flash.display.Loader;
import flash.display.Bitmap;
import flash.events.Event;
import flash.net.URLRequest;
public class BitmapLoader extends Sprite
{
public var bitmapWidth:Number = 0;
public var bitmapHeight:Number = 0;
private var loader:Loader = new Loader();
private var movable:Boolean = false;
private var invisible:Boolean = false;
public function BitmapLoader()
{
}
public function load(_src:String, _invisible:Boolean = false, _movable:Boolean = false):void
{
movable = _movable;
invisible = _invisible;
loader.contentLoaderInfo.addEventListener(Event.INIT, loaderInit);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete);
var req:URLRequest = new URLRequest(_src);
loader.load(req);
}
private function loaderInit(e:Event):void
{
bitmapWidth = loader.content.width;
bitmapHeight = loader.content.height;
dispatchEvent(new Event("Init"));
}
private function loaderComplete(e:Event):void
{
Bitmap(loader.content).smoothing = true;
if (movable)
Bitmap(loader.content).scaleX = Bitmap(loader.content).scaleY = .99;
if (invisible)
Bitmap(loader.content).alpha = 0;
addChild(loader);
dispatchEvent(new Event("Complete"));
}
public function destroy():void
{
bitmapHeight = 0;
bitmapWidth = 0;
loader.contentLoaderInfo.removeEventListener(Event.INIT, loaderInit);
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loaderComplete);
if (loader.content != null)
Bitmap(loader.content).bitmapData.dispose();
try {
loader.unload();
} catch (e:Error) {
}
loader = null;
}
}
}
Is there any long-term problems with this, memory-wise or anything? How do you guys approach this (how do you use loaders)? What constitutes a “good” class?
/ Scherzo