views:

418

answers:

3

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

A: 

I think you can just add the Loader to the parent display object. For Example,

var gameSwf:Loader = new Loader();
gameSwf.load(new URLRequest("piggybankgame.swf"));
gameSwf.x = 100;
gameSwf.y = 100;
gameSwf.width = 320;
gameSwf.height = 200;
//Maybe make it visible of something     
gameSwf.contentLoaderInfo.addEventListener(Event.COMPLETE, gameLoaded);
gameSwf.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
addChild(gameSwf);

I know for a fact that this works in Flex 3 with the SWFLoader class, which is the Flex equivalent I believe.

goatlinks
Yes you can add the loader directly to it and it s a better way to do it.
Patrick
A: 

After the loading of your swf you have to set it's size to what you want =>

function finishLoading(loadEvent:Event) {
 var dob:DisplayObject= loadEvent.currentTarget as DisplayObject;
 // or
 // var dob:Loader = loadEvent.currentTarget as Loader;
 addChild(dob);
 dob.width=myWidth;
 dob.height=myHeight; 
}

Of course the swf you are loading can doing what it wants with the width and the height you are passing.

Patrick
A: 

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.

richardolsson
This is exactly what my problem was. Because I have embedded them into html files before and had no problem I just resumed that the exported swf dimensions were a crop boundary. I cropped my original game and it's working fine now. Thanks for your help.
flashey
Actually Richard I have another question if you'd be able to help me. When you click on the link for the game comes up just fine and now I'm trying to prepare for getting rid of it for when people navigate to other pages. My question if how do I refer to the gameSwf to remove it on all my other links in the code when at the start it has not been added to the stage? I'm sure this a really silly question but I can't work it out. Thanks.
flashey
Just use gameSwf.unloadAndStop() to unload the content and stop all playback therein, and gameSwf.parent.removeChild(gameSwf) to remove it from the display list.
richardolsson
Flash is having a problem with unloadAndStop. It gives up the error 1061: Call to a possibly undefined method unloadAndStop through a reference with static type flash.display:Loader. The gameSwf.parent.removeChild(gameSwf) is working now but I can still hear the music from the game. Thanks again for the help!
flashey
What Flash Player version are you using? Loader.unloadAndStop() was added in Flash Player 10. It was created for this exact reason.The only way to stop music and other gameplay elements in the loaded SWF, if you don't have unloadAndStop(), is to either use some API that is made available by the game SWF, or to loop through all display objects and stop them. That won't necessarily stop sound though.
richardolsson