Movie & Image Preloader Woes

Yes, by now we should all know that Flash MX can load .jpg files dynamically into a flash movie aswell as being able to load external .swf files, but as always, when it comes to loading large files from external sources, it is good practice and almost standard to create a preloader for the file. The new Flash MX Event Model may be causing you problems when it comes to preloading files loaded into your main movie using the Movieclip.loadMovie method or the loadMovie and loadMovieNum functions....

To fully understand this article and the terms used within you might need to touch up on your Flash MX Event Model knowledge.

The Macromedia Flash Help Files 'forgot' to mention that when you are loading an external movie or an external image into a Movieclip, that the Movieclip is reset or emptied of all its contents, this means that all properties, methods, child movieclips and event handlers assigned inside of that Movieclip are deleted and no longer exist.

Once again, remember that levels are also movieclips, _level0 (root timeline) is classed as a Movieclip. Therefore loading an external image or external movie into _level0 using the loadMovieNum function will delete all of the contents of your main movie:

Actionscript:
  1. loadMovieNum("http://tinyurl.com/8lvcg",0);

This can cause problems when it comes to creating preloaders for external movies and external images. The Flash MX Event Model provides us with two new event handlers:

Movieclip.onData
Movieclip.onLoad

According to the Macromedia Flash MX Actionscript Manual, the Movieclip.onData event handler, should fire whenever a data stream is recieved and downloaded into the Movieclip which the Movieclip.onData event handler resides. Unfortuneatley, that is far from true, and to prove that claim here is two lines of code:

Actionscript:
  1. this.onData=function(){
  2. trace('Data was recieved');
  3. }
  4. loadMovieNum('http://tinyurl.com/8lvcg',0)

After reading the Macromedia Flash MX Actionscript Manual, i would expect the above code to output the words "Data was recieved" everytime a piece of the Macromedia Global Navigation swf file had downloaded into the movie, but because of the drawbacks i explained previously it doesnt. The Movieclip.onData event handler is deleted when the loadMovieNum function is called, therefore you cannot use the Movieclip.onLoad event handler to create a preloader for a loaded movie or a loaded image.

The same problem occurs when using the Movieclip.onLoad event handler, according to the the Macromedia Flash MX Actionscript Manual, the Movieclip.onLoad event handler should fire when an external image or external movie has finished loading into the Movieclip which the Movieclip.onData event handler resides. Again, this is so not true, and two lines of code to prove that claim aswell:

Actionscript:
  1. this.onLoad=function(){
  2. trace('Movie was loaded');
  3. }
  4. loadMovieNum('http://tinyurl.com/8lvcg',0);

I would expect the above code to output the words "Movie was loaded" when the Macromedia Global Navigation swf had completley downloaded into the Movieclip, but no, it doesnt output anything, because the Movieclip.onLoad event handler is deleted when the loadMovieNum function is called.

Because of these drawbacks, you will have to use the Movieclip.onEnterFrame event handler of a Movieclip other than the Movieclip which is having either an external movie or an external image loaded into it, to create a preloader:

Actionscript:
  1. //create a new movieclip to load
  2. //the external movie into
  3. this.createEmptyMovieClip('holder',1);
  4. //load the external movie into the new movieclip
  5. holder.loadMovie('http://tinyurl.com/8lvcg');
  6. //check the download status of the external
  7. //movie once every frame
  8. this.onEnterFrame=function(){
  9. //trace the percentage of the movie that has loaded
  10. percent=(this.holder.getBytesLoaded()/this.holder.getBytesTotal())*100;
  11. if(!isNan(percent)){
  12. trace(percent+'% loaded');
  13. }else{
  14. trace('0% loaded');
  15. }
  16. if(percent == 100){
  17. delete this.onEnterFrame;
  18. }
  19. }

You can also fix this bug, by placing this cleaner/tidier code on the first frame of your Flash Movies:

Actionscript:
  1. _global.s_onLoad=function(f)
  2. {
  3. if(onLoadManager == undefined)
  4. {
  5. _global.onLoadManager={};
  6. }
  7. onLoadManager[this] =f;
  8. }
  9. _global.g_onLoad=function()
  10. {
  11. return onLoadManager[this];
  12. }
  13. _global.s_onData=function(f)
  14. {
  15. if(onDataManager == undefined)
  16. {
  17. _global.onDataManager={};
  18. }
  19. onDataManager[this]=f;
  20. }
  21. _global.g_onData=function()
  22. {
  23. return onDataManager[this];
  24. }
  25.  
  26. MovieClip.prototype.addProperty('onLoad', g_onLoad, s_onLoad);
  27. MovieClip.prototype.addProperty('onData', g_onData, s_onData);

Thanks to Bokel @ FlashCoders, for sharing this fix.

Comments are closed.