Skip to main content
Participant
March 12, 2013
Answered

Reusing image from loader - how to duplicate image into a sprite

  • March 12, 2013
  • 1 reply
  • 608 views

Hello.

I'd like to be able to load an external image and then use the loaded image multiple times within a sprite.

So, from what I can make out, I need to get the image out of the Loader somehow and use it as a Bitmap.

Have been looking for a solution for this for hours now, and attempting every possible permutation I could think of, but have hit a real dead end.

Am really hoping someone can point me in the right direction.

Here's my code:

import flash.display.Bitmap;

var sp:Sprite = new Sprite();

addChild(sp);

var bitmapData:BitmapData;

var imageURLRequest:URLRequest = new URLRequest("images/myimage.png");

var loader:Loader = new Loader();

loader.load(imageURLRequest);

loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);

function imageLoaded(e:Event):void {

   bitmapData = e.target.content.bitmapData;

   var myBitmap:Bitmap = new Bitmap;

   myBitmap.bitmapData = bitmapData;

   for (var i:int=1; i<3; i++) {

myBitmap.x = 300* (i-1);

sp.addChild(myBitmap);

}

}

Any ideas how I can fix this so that the loaded image outputs 3 times in the sprite?

Many thanks.


This topic has been closed for replies.
Correct answer kglad

use:

import flash.display.Bitmap;

var sp:Sprite = new Sprite();

addChild(sp);

var bitmapData:BitmapData;

var imageURLRequest:URLRequest = new URLRequest("images/myimage.png");

var loader:Loader = new Loader();

loader.load(imageURLRequest);

loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);

function imageLoaded(e:Event):void {

   bitmapData = e.target.content.bitmapData;


   for (var i:int=1; i<3; i++) {

   var myBitmap:Bitmap = new Bitmap();

   myBitmap.bitmapData = bitmapData;

myBitmap.x = 300* (i-1);

sp.addChild(myBitmap);

}

}


1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
March 12, 2013

use:

import flash.display.Bitmap;

var sp:Sprite = new Sprite();

addChild(sp);

var bitmapData:BitmapData;

var imageURLRequest:URLRequest = new URLRequest("images/myimage.png");

var loader:Loader = new Loader();

loader.load(imageURLRequest);

loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);

function imageLoaded(e:Event):void {

   bitmapData = e.target.content.bitmapData;


   for (var i:int=1; i<3; i++) {

   var myBitmap:Bitmap = new Bitmap();

   myBitmap.bitmapData = bitmapData;

myBitmap.x = 300* (i-1);

sp.addChild(myBitmap);

}

}


higgleAuthor
Participant
March 12, 2013

Ah, you understood exactly what I was trying to achieve! Wasn't sure if I'd been clear enough.

Just a subtle change of approach, makes all the difference. Is great to be able to borrow another pair of eyes/set of braincells.

Thanks so much!

kglad
Community Expert
Community Expert
March 12, 2013

you're welcome.