PDA

View Full Version : How to display the same image twice?



brunoja
January 16th, 2010, 12:55 PM
Hello!

I would like to know how could I display the same image I loaded (with Loader) twice on the screen. AddChild dont accept repeated instances, what should I do? Copy the Image? Wont that be slow?
What if I have the GIFPlayer class, I loaded a gif with it, I want to add it twice on the screen, loading more than once could be slow! What should I do?

I searched and found that for Bitmaps I can do this:


public function duplicateImage(target:Bitmap):Bitmap
{
var bitmapCopy:Bitmap = new Bitmap(target.bitmapData);
return bitmapCopy;
}But I didnt tested it to see if its fast enought to create when the game is running.

With gifs, I found a solution, to use the GIFPlayer.loadBytes(), so it will create another GIFPlayer. BUT, if I do this while the game is playing I notice that it is slow, my game hangs when it creates a "copy" when dealing with larger gifs.

I dont know how to solve this, I cant add the same object instance twice to the display list. I was used to program in C, using SDL. There was a function that draws things to screen using a reference to the bitmap data, so I could draw thousands of the same images on the screen with only one bitmap instace :/

The GIFPlayer I got from here: http://www.bytearray.org/?p=95

Anyway to solve this? Thank you :p

creatify
January 16th, 2010, 04:18 PM
EDIT: If you're using animated gifs, then my solution below probably won't work in your case - you'd probably have to hack into the GifPlayer items that you've played with already.




// load your gif
var _loader:Loader = new Loader();
_loader.contentLoaderInfo.addEventListener(Event.C OMPLETE, completeHandler);
_loader.load(new URLRequest("somegif.gif"));

// these will be references to the original and it's duplicate
var _yourGif:Bitmap;
var _duplicateGif:Bitmap;

function completeHandler(e:Event):void {
// add the loaded gif to the stage
_yourGif = e.target.content as Bitmap;
addChild(_yourGif);
// can then call a duplicate function
makeDuplicate();
}

function makeDuplicate():void {
// here, we create a new BitmapData instance.
var bmpd:BitmapData = new BitmapData(_yourGif.width, _yourGif.height, false, 0xFFFFFFFF);
// here we draw the "contents" of your original item onto this bitmapData
bmpd.draw(_yourGif.bitmapData);
// bimapData, in your case, needs to go into a Bitmap instance to place it on the stage
_duplicateGif = new Bitmap(bmpd);
_duplicateGif.x = _duplicateGif.y = 100;
addChild(_duplicateGif);
}



http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/display/BitmapData.html
http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/display/Bitmap.html

Hope this helps.

brunoja
January 18th, 2010, 08:32 AM
Thank you, it works. Now I have a problem, the GIFPlayer when I use the load function in the gameplay (ex: while figthing I ceate new monster), it hangs my game with larger gifs :/

mathew.er
January 18th, 2010, 11:04 AM
Don't use BitmapData.draw, it's rather slow. The code in your first post (creating a new Bitmap and passing it BitmapData from another Bitmap) should be quicker, but I guess you can't modify the BitmapData pixels without the changes reflecting in the clones too.

edit: as the docs say:

A Bitmap object can share its BitmapData reference among several Bitmap objects, independent of translation or rotation properties. Because you can create multiple Bitmap objects that reference the same BitmapData object, multiple display objects can use the same complex BitmapData object without incurring the memory overhead of a BitmapData object for each display object instance.