Stage resizing and getting the right variable
Asked Answered
P

1

3

In my Flash Application (AS3), I want to get the stage size and use that so my objects (which the user controls) can't go outside of the screen. However, when I use:

stage.stageWidth;
stage.stageHeight;

The values I get aren't actually what I want. They give me the stage size, but if I resize the window, the numbers change as well. Now, in a html page, I don't think that will matter, because the user can't resize it... however, I'd like for it to be more solid than relying on the window size. I also tried to use:

stage.width;
stage.height;

However, those gave me "801" and "601", one greater than my actual stage size. Those values did not change when I resized the window, but they were one greater.

So my question is, how can I get the stage size, regardless of resizing the window to always be 800 by 600? (And perhaps hidden in that question is, "Why do I get 801 and 601?" and "Can I stop people from resizing the window?") Phew! Thank you!

Profundity answered 20/1, 2011 at 23:19 Comment(0)
B
9

First, it is important to know the difference between stage.stageHeight - stage.stageWidth and stage.height - stage.width

stage.stageHeight // The real height of the stage
stage.height // The height of the content on the stage

So, the reason you get 801 and 601 is because the height and width of the total content on the stage is 801 x 601.

Now, for the resizing issue. If you are displaying this in a webbrowser, then you can set the height and width of the movie. Set that to 800 and 600, not 100% and 100%. Now, your movie will always stay the same size, regardless of a user resizing their browser window.

If you do want to allow for height="100%" and width="100%" in your html markup, then you have a couple options.

  1. Make a MovieClip that will act as a pseudo-stage. It would be 800 x 600. Instead of adding things to the stage, add them to this MovieClip (say it is called myStage.) Now, all of the logic for keeping the objects inside the stage is instead applied to the myStage MovieClip.

  2. You can put constants in your AS3 code. For example:


 public const STAGE_WIDTH:int = 800;
 public const STAGE_HEIGHT:int = 600;

Now, you just use those values rather than your stage.stageHeight and stage.stageWidth values. Of course, you need to be sure to also include

stage.align = StageAlign.TOP_LEFT;  // or StageAlign.TOP
stage.scaleMode = StageScaleMode.NO_SCALE;
Backus answered 20/1, 2011 at 23:59 Comment(1)
I know this is quite a few years old, but I'm messing around with this and I must be missing something: In my HTML page I specify the height and width with constants (<object width="320" height="240">) and indeed in my browser the Flash object is displayed in a box that size. However, when I inspect stage.stageWidth and stage.stageHeight I get 800x600. What is going on?Murmuration

© 2022 - 2024 — McMap. All rights reserved.