Question

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!

Was it helpful?

Solution

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top