PDA

View Full Version : Converting bitmapdisplay object to sprite (Please help)



adnan794
May 18th, 2008, 03:31 PM
Hi

I am loading jpg image and then trying to assign that image to a sprite object

I have a class that is loading the image and it has a method that returns the loaded image this class is called ImageLoader and the code that loads and returns the image is given under



//ImageLoader class
private var loadedImage:Object;

public function loadImage(image:String):void
{
urlRequest = new URLRequest(image);
loader.contentLoaderInfo.addEventListener(Event.IN IT, initListener);
loader.load(urlRequest);
}
public function initListener(e:Event):void
{
loadedImage = new Object();
loadedImage = loader.content;
dispatchEvent(new Event(Event.COMPLETE));
}
public function getImage():Object
{
return loadedImage;
}



I have another class content that calls the getImage method of ImageLoader when the complete event is dispatched the code from that class is given under



//Content class

var s:Sprite = new Sprite();

// onComplete event


// this gives an error saying TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Bitmap@33d3779 to flash.display.Sprite.

s = Object(ImageLoader.getImage());




Any idea on how to solve this problem

Thanks in advance

Felixz
May 18th, 2008, 03:45 PM
images are Bitmap class which is different from Sprite because Sprite also extends InteractiveObject, Bitmap doesn't.
Instead casting Bitmap to Sprite use
var s:Sprite = new Sprite();
s.addChild(ImageLoader.getImage());

adnan794
May 18th, 2008, 03:50 PM
Hi yea I kind of sorted the problem out and just for others who are interested in this post what i did is insteading of assigning bitmap data to the sprite I have added that as a child of the sprite.

_imageSprite.addChild(ImageLoader.getInstance().ge tImage());

adnan794
May 18th, 2008, 03:51 PM
Ps I have made the loadedImage a displayObject cuz Bitmap is really a display object

Any further questions please do not hesitate to contact