How can I get the width/height of a loaded swf's stage in AS2?

loopj picture loopj · Jun 2, 2009 · Viewed 10.8k times · Source

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

Answer

Luke picture Luke · Jun 2, 2009

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);
}