Anthony Miller
Anthony Miller

Reputation: 15920

How to create a sprite from an image loaded in Loader()?

I'm loading a background image via the Loader() class and wanted to know if there's a way to create a sprite from that loaded image?

I'm wanting to put a function in an external class file to put the image in the loader and then call the class to create a sprite from the loaded image. I'm not even sure this is possible.

Note: I'm using flashdevelop and no timeline.

Upvotes: 0

Views: 6094

Answers (1)

Barış Uşaklı
Barış Uşaklı

Reputation: 13532

You can just use the loader object as a display object or you can access the Bitmap object in the loader and add that to a sprite.

 var loader:Loader = new Loader();
 loader.load(new URLRequest(filename)); 
 addChild(loader);

 loader.x = 100;
 loader.y = 200;
 //so on

To get access to the bitmap and bitmapdata loaded just add an event listener and access them.

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
loader.load(new URLRequest(filename));  

private function onLoadComplete(e:Event):void 
{
   var loaderInfo:LoaderInfo = e.target as LoaderInfo;
   var loadedBitmap:Bitmap = loaderInfo.content as Bitmap;

   var sprite:Sprite = new Sprite();
   sprite.addChild(loadedBitmap);

   addChild(sprite);

   sprite.x = 100;
   sprite.y = 200;
   //so on

}

Upvotes: 4

Related Questions