Duplicating an imported graphic in Flash
-
22-09-2019 - |
Question
I'm loading a graphic via the Loader Class. Now I need to use it both as the original image and a thumbnail of that image. Alas, there is no "duplicateMovieClip" or anything like that in AS3
If I addChild it to the normal view and then to the thumbnail only the thumbnail is shown and vice versa.
I google for this and found several solutions online, but they all just seem to work with Images from the library and not loaded from a Server.
So, how do I do this without having to load the Image twice?
Solution
If were are just talking about a bitmap image, the simplest thing is just to share the BitmapData with another Bitmap instance. See below:
var existingBitmap:Bitmap; //which you have from the loader
var thumbNail:Bitmap = new Bitmpap(existingBitmap.bitmapData);
thumbNail.witdth = 64;
thumbNail.height = 64;
addChild(thumbNail);
Since you are using a loader, you can access the bitmap image you loaded externally through the content
property.
var existingBitmap:Bitmap = myLoader.content;
OTHER TIPS
Depending on what you need to do with it you have three options.
- Load it again. By far the easiest, it's going to be cached anyway so you won't need to hit the server twice.
- Use BitmapData.draw(). Create a bitmapdata and draw your loader to it, also pretty easy, but you can't do any animation without having to redraw.
- Load it and wrangle out the class of your graphic (only applies to .swf's) and use that to instantiate copies. Take a look at getDefinitionByName. This is a little bit tricky since you are at the mercy of the sandbox, but it's also the most proper way to do it.