Unified Flash 6 - Flash 10 Loader

28.05.2008 3428 2

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.

Commentaires

23.07.2008 à 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?

23.07.2008 à 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.

Laisser un commentaire

http://
×