Using the Loader class in AS3

Using the Loader class in AS3

I recently worked on a project that required the user to upload a photo of themselves via a web form, and this photo would then be used in a Flash animation.

This required using the Loader class that loads external assets (such as images and Flash files) at run time into a Flash movie. For more information about using the AS3 Loader class, take a look at the fantastic tutorial at Republic of Code. Below is a shortened example of how the image was loaded.

The first line of our code simply created an instance of the Loader Class, the instance name is my_loader.

The second line loaded the external image using the .load() method. This method requires the URL to be provided to it through the URLRequest Class. The URLRequest can be instantiated through different methods, the one shown in our code below is the shortest and easiest one to use.

var my_loader:Loader = new Loader();
my_loader.load(new URLRequest("photo.jpg"));

This instance of the Loader class was then used in two different MovieClips at different times during the Flash animation. For simplicity, I'll refer to these MovieClips as MovieClip A and MovieClip B.

The first frame of MovieClip A and B, adds the instance of the Loader class to the respective MovieClip so that we can see the image on the screen.


MovieClip A displayed the instance of the Loader class without any issues. For a reason I am not 100% certain about, the Loader class instance in MovieClip B would either flicker or not display at all.

I tried using the CacheAsBitmap property, but as I was altering the position of the Loader instance in a multi-fram animation, this actually had an adverse effect on the performance of the movie.

After many hours of trying to find a solution, I decided upon having 2 Loader instances with the same image.

var my_loader_1:Loader = new Loader();
my_loader_1.load(new URLRequest("photo.jpg"));
var my_loader_2:Loader = new Loader();
my_loader_2.load(new URLRequest("photo.jpg"));

If anyone has any ideas about why this was happening, please feel free to add a comment below.