Näbil Y
Näbil Y

Reputation: 1650

Get dimensions of stage

I want the full dimensions of my "stage" or document or whatever it's called. I just started with flash and I've been looking all over with no success.

The closest I came was "stage.height", but it does not do what I wanted. I realized "stage.height" returns 2x size of my object. What I'm trying to achieve is to make sure the object cannot go outside the screen. I use their example movement actionscript code. I attempted to modify it to fit my needs, this is the edited code (where stage.height not working as expected nor width)...

function fl_MoveInDirectionOfKey(event:Event)
{
    if (upPressed)
    {
        Ball.y = Math.max(0, Ball.y - 5);
    }
    if (downPressed)
    {
        Ball.y = Math.min(Ball.y + 5, stage.height);
    }
    if (leftPressed)
    {
        Ball.x = Math.max(0, Ball.x - 5);
    }
    if (rightPressed)
    {
        Ball.x = Math.min(Ball.x + 5, stage.width);
    }
} 

Upvotes: 0

Views: 368

Answers (1)

MRS1367
MRS1367

Reputation: 1053

You should use "stage.stageWidth" for width of stage or "stage.stageHeight" for height of stage.

Remember that if you use nested movie clips into your flash project, and you want to control some objects in them; it's better x and y coordinates of your movie clips that contain your objects set in zero values; if x and y coordinates is set to another value, you must find some calculations for achieve to what you want to do.

If you want to know about differences between stage.stageWidth and stage.width or stage.stageHeight and stage.height, please check following links:

Difference between calling stage.width and stage.stageWidth?

Difference between stage.width and stage.stageWidth

For better understanding, I describe it with an example.

Imagine that we have a stage with 1024px width and 768px height. We create a movie clip symbol onto the stage in 30px x and 70px y coordinates. Now we add something in that movie clip and set x and y coordinates to zero value. If we return to root timeline (main stage), object of movie clip is set in 30px x and 70px y coordinates not in 0. If we want to set x and y coordinates of the object to x and y coordinates of the stage, we must set -30px for x and -70px for y. in this situation, we have to do some calculations for correct control on our object in the movie clip for what we want to do.

I'm created an example for you. Please check it:

Objects Movement

If you want that your objects remain on the stage when stage resizes, you must change "Scale Mode" of stage to No Scale and "Align" of stage to "Top Left" as follows:

import flash.display.StageScaleMode;

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

and then, you must add "Resize" event to your stage.

import flash.events.Event;

stage.addEventListener(Event.RESIZE, stage_Resize);

function stage_Resize(event:Event):void
{
    enter your codes here
}

With adding Resize event, when stage resizes by end-users, your codes in the Resize event will be fired.

Please check following example:

4Shared -> Resize Example

SendSpace -> Resize Example

Upvotes: 2

Related Questions