Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
Mar 12, 2013 Mar 12, 2013

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.


TOPICS
ActionScript
557
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 12, 2013 Mar 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;

m

...
Translate
Community Expert ,
Mar 12, 2013 Mar 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);

}

}


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 12, 2013 Mar 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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 12, 2013 Mar 12, 2013
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines