PDA

View Full Version : How to center the loaded .swf using as3?



juliecutey
January 23rd, 2008, 04:14 PM
Hi everyone,

I am wondering is there a way to center the loaded .swf(external swf) using as3 and it will still stay centered when the browser is resized? Any help would be really appreciated. Thank you very much!

Julie

aBnest
January 24th, 2008, 05:27 AM
Hello Julie,

this is how i would do it:



package {

import flash.display.Sprite;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.StageAlign;
import flash.display.StageScaleMode;

public class Main extends Sprite{

private var container:Sprite;

public function Main(){

container = new Sprite();
this.addChild(container);
var url:URLRequest = new URLRequest("inside.swf");
var loader:Loader = new Loader();
loader.load(url);
loader.contentLoaderInfo.addEventListener(Event.CO MPLETE, completeHandler);

this.stage.scaleMode = StageScaleMode.NO_SCALE;
this.stage.align = StageAlign.TOP_LEFT;
}

private function completeHandler(event:Event):void{
this.container.addChild(event.target.content);
this.container.x = this.stage.stageWidth/2 - event.target.width /2;
this.container.y = this.stage.stageHeight/2 - event.target.height /2;

this.stage.addEventListener(Event.RESIZE, resizeHandler);
}

private function resizeHandler(event:Event):void{
this.container.x = event.target.stage.stageWidth/2 - event.target.width /2;
this.container.y = event.target.stage.stageHeight/2 - event.target.height /2;
}
}
}

juliecutey
January 25th, 2008, 12:17 PM
Hi aBnest,

Thank you so much~~~. I will definitely try it out and let you know. :)

Julie

juliecutey
January 25th, 2008, 12:27 PM
Hi aBnest,

It works out great! You are so good. :b: :thumb:

arsenalfc
August 6th, 2009, 12:32 PM
This works a treat in flash player but I can't get it to work in the browser after I've published the HTML from flash.

Can any one help me with that?

Thanks

fusionPT
August 6th, 2009, 02:49 PM
Is the flash 100% width and height?

alejandroy
February 16th, 2010, 09:22 PM
Sorry for hijacking the thread but....

Let's say i want to go to different frames, and load different swf's? I see your class only allows me to call one swf. I'm asking because I'm having real trouble with a project that requires me to use AS3 and I'm a complete AS3 noob.

If you could go to www.designsuccess.com/proof (http://www.designsuccess.com/proof) you will see what my problem is. Try to navigate to any section, no problem so far. Then try to use the auxiliary menu on the right and the buttons just won't work. I think the problem has to do with some code that I had to include in the external swf's in order for them to work.

While testing locally (without actually loading any swf's) the buttons indeed make the timeline jump to the specified frame. The problems begin once I load the external swfs. I start getting the infamous 1009 error and the buttons stop working.

I have really run out of ideas and I have searched for a solution, but I juts canīt find it. If anyone is willing to help, i can send you the source files.

Thanks,