Say I loaded an image into my container movieclip that I wanted to use
multiple times in my movie. Easy. duplicateMovieClip(). Wrong. "If you
used MovieClip.loadMovie() or the MovieClipLoader class to load a movie
clip, the contents of the SWF file are not duplicated. This means that
you cannot save bandwidth by loading a JPEG, GIF, PNG, or SWF file and
then duplicating the movie clip." Well that seems very limiting but oh
well. So what's the work-around? The BitmapData Object that was
introduced in Flash 8. We load our image using the MovieClipLoader
Class, use the BitmapData Object to draw an instance of our image,
remove the movieclip that our image was loading into and Flash has
stored our image as a bitmap which can be used as many times as we like.
First thing we need to do is import the BitmapData class
import flash.display.BitmapData;
This is simply a temporary movieclip that we will later remove altogether
_root.createEmptyMovieClip("tempMC", 1, {_alpha:0});These next several lines are simply setting up our MovieClipLoader
var theLoader:MovieClipLoader = new MovieClipLoader();
var theListener:Object = new Object();
theLoader.addListener(theListener);
This next line initiates our function as soon as the image has finished downloading and is ready to receive events
theListener.onLoadInit = function(mc:MovieClip) {
This creates a new BitmapData Object and then copies the image that we
have loaded into that Object and finally removes the movieclip that we
loaded our image into.
bitmapPic = new BitmapData(_root.tempMC._width, _root.tempMC._height);
bitmapPic.draw(mc);
_root.tempMC.removeMovieClip();
Now we will simply attach our BitmapData Object
to a new movieclip, which you can do as many times as you want and we
are finished.
_root.createEmptyMovieClip("picMC", 2);
_root.picMC.attachBitmap(bitmapPic, 3);
};
One last thing. We need to load our image into the temporary movieclip that we setup
theLoader.loadClip("http://www.yourwebsitehere.com/yourimagenamehere.jpg", _root.tempMC);So that was relatively painless and can open up some new opportunities when using dynamically loaded images in Flash.
And for those that just want to copy and paste the code directly into Flash here you go...
import flash.display.BitmapData;
_root.createEmptyMovieClip("tempMC", 1, {_alpha:0});
var theLoader:MovieClipLoader = new MovieClipLoader();
var theListener:Object = new Object();
theLoader.addListener(theListener);
theListener.onLoadInit = function(mc:MovieClip) {
bitmapPic = new BitmapData(_root.tempMC._width, _root.tempMC._height);
bitmapPic.draw(mc);
_root.tempMC.removeMovieClip();
_root.createEmptyMovieClip("picMC", 2);
_root.picMC.attachBitmap(bitmapPic, 3);
};
theLoader.loadClip("http://www.yourwebsitehere.com/yourimagenamehere.jpg", _root.tempMC);