How can I get the width/height of a loaded swf'

2019-07-16 02:14发布

问题:

I'm using MovieClipLoader to load an external as2 swf file into my as2 flash project, and I'm having trouble getting the original stage size of the loaded swf.

When I run the following code:

var popup:MovieClip = _root.createEmptyMovieClip("popup", 1);
var loader:MovieClipLoader = new MovieClipLoader();
var loadHandler:Object = new Object();
loader.addListener(loadHandler);
loader.loadClip(url, popup); 
loadHandler.onLoadInit = function(mc:MovieClip) {
    trace(mc._width + ", " + mc._height);
}

I get strange width/height values (mc._width=601.95, mc._height=261.15) when what I actually want is the stage size of the loaded swf file, which in this case I know to be 300px x 250px.

Any suggestions appreciated! Thanks

回答1:

The problem here is that the loaded swf looses it's stage size when it's loaded into another swf. The stage of the parent becomes the stage of loaded swf. When requesting the size of the loaded swf, like you do, it will return the width and height of the entire surface of the first frame, not that of the stage.

The way I've solved this in the past is to create a movieclip instance on the first frame of the loaded swf with the size of the stage of that swf. Once the swf has been loaded you can then target that swf and get it's dimensions. Of course, this only works if you have publishing control over the swf you're trying to load.

To illustrate this in an example. In your swf to be loaded place a movieclip (e.g. a movieclip of a rectangle) on the first frame and name it stage_mc. When you now load the swf you can target that stage_mc instance like so:

loadHandler.onLoadInit = function(mc:MovieClip) {
    trace(mc.stage_mc._width + ", " + mc.stage_mc._height);
}


回答2:

In AS3 you CAN get the height of a loaded SWF with the loaderinfo property:

// imports
    import flash.display.LoaderInfo;

// loading code
    var loader:Loader = new Loader();
    loader.load(new URLRequest('some_swf.swf'));
    loader.contentLoaderInfo.addEventListener(Event.INIT, loaderInitHandler);

// listener
    function loaderInitHandler(event:Event):void 
    {
        var info:LoaderInfo = event.target as LoaderInfo;
        trace('Loaded swf is ' + info.width + ' x ' + info.height + ' px');
    }

// Loaded swf is 500 x 300 px


回答3:

try

stage.width; 
stage.height;