Copy link to clipboard
Copied
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.
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;
m
Copy link to clipboard
Copied
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);
}
}
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
you're welcome.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now