Hello
I have a class name ImageLoader that is used to load image and the dispatch a complete event when the image is loaded. This class is implementing singleton design pattern which means that only one instance is created always for this class.
Everything is working fine it is loading the image but in the initListener function i am dispacthing a complete event to let the other class Content (which passes the image name to this class ) that the image is loaded and you can access it now.
BUt the problem is the event is never dispatched or may be it is but I am not catching it properly here is code for my ImageLoader class.
package
{
import flash.display.*;
import flash.net.*;
import flash.events.*;
public class ImageLoader extends Sprite
{
private static var _instance:ImageLoader;
private static var loadedImage:DisplayObject;
private static var loader:Loader;
private static var urlRequest:URLRequest;
public function ImageLoader(ImageLoaderEnforcer:ImageLoaderEnforcer)
{
}
public static function getInstance():ImageLoader
{
if (ImageLoader._instance == null)
{
ImageLoader._instance=new ImageLoader(new ImageLoaderEnforcer );
loader = new Loader();
loadedImage = new Sprite();
}
return ImageLoader._instance;
}
public function loadImage(image:String):void
{
urlRequest = new URLRequest(image);
loader.contentLoaderInfo.addEventListener(Event.INIT, initListener);
trace(image);
loader.load(urlRequest);
}
public function initListener(e:Event):void
{
loadedImage = DisplayObject(loader.content);
dispatchEvent(new Event(Event.COMPLETE));
}
public static function getImage():DisplayObject
{
return loadedImage;
}
}
}
class ImageLoaderEnforcer
{
}
in the other class content I am using the following code to register for complete event.
// Content class
ImageLoader.getInstance().loadImage("image1.jpg");
ImageLoader.getInstance().addEventListener(Event.COMPLETE, onComplete);
public function onComplete(e:Event):void
{
trace("Complete");
}
Any help will be aprreciated