views:

458

answers:

2

I have several components who's screen position depends on the resolution of the monitor on which the browser lives.

loginBox.x = (flash.system.Capabilities.screenResolutionX - loginBox.width) / 2; loginBox.y = (flash.system.Capabilities.screenResolutionY - loginBox.height) / 2;

The problem I'm encountering is that the flash.system.Capabilities method pulls the resolution of the primary monitor attached to the computer. In most situations this isn't a problem but on one of my computers, I have a 1680x1050 and a 1440x900. At work, I have a 1920x1200 and a 1680x1050, so if I open the page in a browser on the smaller monitor, things are not centered and my tools panel is completely off to the right of the screen.

I have a block of javascript that I've tried, both in html and through php but the problem is that if I use either httpRequest or urlLoader to grab the html file, I get the source of html and if I try the php script, I get a script block that is attempting to write a cookie. If that page has not been visited prior to loading the flash site, or if cookies are disabled, it never gets written and I'm nowhere.

Is there a method that I can use, that doesn't rely on cookies, to detect the resolution of the monitor that the browser is actually on, and not just the resolution of the first monitor?

A: 

If you're trying to communicate between flash and javascript, ExternalInterface is the way to go. Rather that writing a cookie, just grab the information from javascript, and send it to flash via a callback.

Breton
+1  A: 

Why don't you use stage.stageWidth and stage.stageHeight for positioning purposes?

EDIT: Ok, here's an example. I'm considering that you'll place this code inside the container where loginBox also resides. The stage property is undefined unless your object is added to the DisplayList, so you need to listen for when it's added and then position the loginBox.

// in the constructor
this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

and then

private function onAddedToStage(evt:Event) {
    loginBox.x = (stage.stageWidth - loginBox.width)/2;
    loginBox.y = (stage.stageHeight - loginBox.height)/2;
}

Also, if you have stage scaleMode set to noScale (which maybe is the case), if you need to constantly reposition the loginBox based on browser resizing, you need to listen to an Event.RESIZE event

stage.addEventListener(Event.RESIZE, onResize);
Virusescu
I don't know why, but I have an aversion to JavaScript and avoid it where possible. I went through my code and got stage.stageHeight to work. Sort of. I was getting an error message for an invalid property or method, so I added the following:override protected function updateDisplayList( unscaledWidth:Number, unscaledHeight:Number ) : void{ super.updateDisplayList( unscaledWidth, unscaledHeight ); myStageWidth = unscaledWidth; myStageHeight = unscaledHeight;}I'll have to try it on my desktop and see if everything is healed.
Charles Shoults
before accessing the stage, it must be added to the display list. You must first listen for Event.ADDED_TO_STAGE. Inside of the method that fires when Event.ADDED_TO_STAGE is dispatched you may then use stage.stageWidth; Otherwise you will get an error. A common work around is to check stage in the constructor, if you cannot access it, or it is null, then you listen for Event.ADDED_TO_STAGE, both resulting in an init function.if(stage) _init();else addEventListener(Event.ADDED_TO_STAGE, _init);private function _init(e:Event = null):void{ //You may now access stage}
Brian Hodge
Part of my structure has an application loaded through the use of an SWFLoader. I found that this sub application detected width as 1440 but height as 802. I'm guessing that has to do with toolbars. That application also loaded a gameplay demo into itself, but this one detected the width as 500 because it's container didn't have dimensions specified. I started having the primary application do the check and distribute the numbers using LocalConnection, but in the end, used a Shared Object and set up the other two applications to access it as needed.
Charles Shoults