R – Problems with dimensions of swf when loaded into fla

actionscript-3flash-cs3

I am creating a portfolio website for myself and want to embed a swf that is a simple flash game into my main flash file. My problem is that when I use this code to put the swf into my main flash file it doesn't keep the proper swf dimensions and shows things that are outside of the stage. I can't seem to figure it out! Any help is much appreciated! Thanks.

Here's my code.(I'm using Flash CS3 with AS3.)

var my_Loader:Loader = new Loader();

var my_url:URLRequest=new URLRequest("piggybankgame.swf");

my_Loader.contentLoaderInfo.addEventListener(Event.COMPLETE, finishLoading);
my_Loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);

my_Loader.load(my_url);

function finishLoading(loadEvent:Event) {
    addChild(loadEvent.currentTarget.content);
}

Best Answer

If I'm understanding you correctly, this has to do with the way that the flash game SWF was created. Most probably, it's built without caring about what goes on outside of the stage boundaries.

A common misconception is that the rectangle defined by the SWF width and height is a crop boundary. It is not. Any content that extends beyond this border will be visible, if the window that displays the SWF is large enough to show it. This also happens when resizing a standalone Flash Player window in your operating system, or when embedding a SWF into a HTML page with width and height set to greater values than what was defined in the SWF (e.g. when publishing from Flash CS3/CS4.)

If you want to load content like a game, into a SWF with larger dimensions, and only show that part of the game SWF that is within it's stage dimensions, you will need to mask it.

var loader : Loader = new Loader();
loader.load(new URLRequest('my320x240game.swf'));

var gameMask : Shape = new Shape;
gameMask.graphics.beginFill(0xffcc00);
gameMask.graphics.drawRect(0, 0, 320, 240);

loader.mask = gameMask;
this.addChild(loader);

This will create a vector shape (a 320x240 rectangle) that acts as a cropping mask for the loader display object. If you are familiar with masks, it's hopefully ease to see how this means that anything extending outside of the 320x240 rectangle will be made invisible.

There are nicer ways to arrange the code of course, but this will hopefully get you going in the right direction.

Related Topic