Movie and Image Preloader Woes

Movie and Image Preloader WoesYes, 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:

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:

this.onData=function(){
trace('Data was recieved');
}
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:

this.onLoad=function(){
trace('Movie was loaded');
}
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:

//create a new movieclip to load
//the external movie into
this.createEmptyMovieClip('holder',1);
//load the external movie into the new movieclip
holder.loadMovie('http://tinyurl.com/8lvcg');
//check the download status of the external
//movie once every frame
this.onEnterFrame=function(){
//trace the percentage of the movie that has loaded
percent=(this.holder.getBytesLoaded()/ this.holder.getBytesTotal())*100;
if(!isNan(percent)){
trace(percent+'% loaded');
}else{
trace('0% loaded');
}
if(percent == 100){
delete this.onEnterFrame;
}
}

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

_global.s_onLoad=function(f)
{
if(onLoadManager == undefined)
{
_global.onLoadManager={};
}
onLoadManager[this] =f;
}
_global.g_onLoad=function()
{
return onLoadManager[this];
}
_global.s_onData=function(f)
{
if(onDataManager == undefined)
{
_global.onDataManager={};
}
onDataManager[this]=f;
}
_global.g_onData=function()
{
return onDataManager[this];
}

MovieClip.prototype.addProperty('onLoad', g_onLoad, s_onLoad);
MovieClip.prototype.addProperty('onData', g_onData, s_onData);

Close    To Top
  • Prev Article-Flash:
  • Next Article-Flash:
  • Now: Tutorial for Web and Software Design > Flash > Basic > Flash Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction