Unified Flash 6 - Flash 10 Loader

05.28.2008 4017 2

Here comes a sample of an universal loader that can load flash movies from version 6 to flash 9 or higher.
In fact, the problem is not really the version number of the flash movie but of the ActionScript Virtual Machine (AVM) used. Flash applications using ActionScript 1/2 (flash <=8) work with AVM1 and these using ActionScript 3 (flash 9-10) work with AVM2, and both are not compatible.
AVM2 have instances of AVM1 and you can load flash<=8  applications into a flash 9 or flash10 but you cannot load flash 9 or 10 into flash <=8 movies... I don't know if it's really clear...
Anyway, it's a flash 6 universal loader with progress event and flashvars, that can load and execute flash 9 or 10 applications.


UL.hx:

import flash.MovieClip;
import flash.TextField;
class UL
{
    var root    : MovieClip;
    var params    : String;
    var url        : String;
    var mc        : MovieClip;
    var t        : haxe.Timer;
    
    function new( url : String )
    {
        root = flash.Lib.current;
        this.url = url;
        
        // Transfering flashvars from universal loader to the loaded movie
        params = "";
        for ( i in Reflect.fields( root ) )
            if (    
                    i != "@closure"        &&
                    i != "@instanceof"    &&
                    i != "@Main"        &&
                    i != "@estr"        &&
                    i != "$version"        &&
                    i != "IntIter"        &&
                    i != "Reflect"        &&
                    i != "Std"            &&
                    i != "flash"        &&
                    i != "haxe"            &&
                    i != "UL"            &&
                    i != "swfURL"
                )
                if ( params == "" )
                    params += "?" + i + "=" + Reflect.field( root, i );
                else
                    params += "&" + i + "=" + Reflect.field( root, i );
        
        // creating percent loading label
        root.createTextField( "tf", 0, 0, 0, 100, 20 );
        root.tf.autoSize = "left";
        
        // creating progress event listener
        t = new haxe.Timer( 25 );
        t.run = oEF;
        
        // loading the movie into a temporary movieclip to get progress event
        mc = root.createEmptyMovieClip( "mc", 1 );
        mc.loadMovie( url + params ); 
    }
    
    function oEF()
    {
        // updating percent loaded
        var percent = Math.round( mc.getBytesLoaded() / mc.getBytesTotal() * 100 );
        if ( !Math.isNaN( percent ) )
            root.tf.text = percent + "%";
        root.tf._x = ( flash.Stage.width - root.tf._width ) / 2;
        root.tf._y = ( flash.Stage.height - root.tf._height ) / 2;
        
        // loading second time the movie onto the root (the only way to switch between AVMs), from the browser cache* 
        if ( !Math.isNaN( percent ) && mc.getBytesLoaded() != 0 && percent >= 100  )
            root.loadMovie( url + params );
    }
    
    static function main()
    {
        var ul = new UL( flash.Lib.current.swfURL );
    }
}

build.hxml :

-swf index.swf
-main UL
-swf-version 6

In order to get the progress event (bytes loaded), we have to load the movie 2 times : once to follow bytes and then to switch the AVMs. But in fact, when using the browser's cache, it looks like you load it just once
Here, the loader takes a parameter from flashvars, named swfURL, that targets the movie to load. The rest of the flashvars are transmited to the target movie.
I found this trick about 2 years ago yet, when trying to load a movie with different framerate that the loader's one and it switched the framerate when it was loaded onto the root stage.

Comments

07.23.2008 at 12:48 Gilles Guillemin

Hi,
I got it working fine loading AS2/Flash8 swf files, but nothing with AS3/Flash9 ones (actually Flex generated swf files).
Also, there's one thing a little bit unclear: in order to load AS3/Flash9-10 swf files, the user must have Flash 9 player or earlier installed even though the Universal Loader is coded in Flash 6, right?

07.23.2008 at 13:02 Michal

Hi, Yes you must have the latest flash player for your application installed to get it working with this universal loader.
Here it's just an example which is compiled into version 6 in order to make it working with old flash files too.

Write a comment

http://
×