Flash Script
none none none
none Stage Width, Height, as3

NO stage.width and stage.height.
This refers to the width of the CONTENT on the stage and NOT the stage itself, i.e. if it has NO display objects on stage (movie clips or sprites) then stage.width would return a zero.

YES stage.stageWidth.
This returns the ACTUAL stage width.



Error Messages
Attempted access of inaccessible method stageWidth through a reference with static type flash.display:Stage.

The cause of the error this.graphics.drawRect(0,0, stage.stageWidth(), stage.stageHeight());
DO NOT USE round parentheses or brackets.
To fix use: this.graphics.drawRect(0,0, stage.stageWidth, stage.stageHeight);

Why did a method call act as an attribute? It was their way to make these methods accessible via the Flex XML document without using inline ActionScript to access those methods.
I dont agree with it, but it helps to understand where this comes from.



Code: Background Fill in Flash as3
Open up your Adobe Flash CS3, CS4, CS5 ...
Keep the default settings, open up the actions window and type the code below:
trace("stage.stageWidth "+stage.stageWidth);
trace("stage.stageHeight "+stage.stageHeight);
graphics.beginFill(0xFF0000,1);
graphics.drawRect(0,0, stage.stageWidth, stage.stageHeight);



Code: stage width, height
What does stage.width and stage.height do.
Best way to explain it is to illustrate it.
Copy and paste this code into the actions window and run it.

var tf:TextField = new TextField();
trace("1. stage.width: "+stage.width+" stage.height: "+stage.height);
addChild(tf);
trace("2. stage.width: "+stage.width+" stage.height: "+stage.height);
tf.text = "The lazy brown fox quickly jumped over the fence";
trace("3. stage.width: "+stage.width+" stage.height: "+stage.height);
tf.x = 50;
trace("4. stage.width: "+stage.width+" stage.height: "+stage.height);
var tf1:TextField = new TextField();
addChild(tf1);
trace("5. stage.width: "+stage.width+" stage.height: "+stage.height);

Output:
1. stage.width: 0 stage.height:0
2. stage.width:100 stage.heigt:100
3. stage.width:100 stage.heigt:100
4. stage.width:100 stage.heigt:100
5. stage.width:150 stage.heigt:100

1. No content on stage, returns width 0, height 0.
2. Once we added a textfield (default width and height of the display object is 100, 100), the stage width and height is 100 by 100.
3. Adding text to the textfield did not change the value, since the textfield object by default does not automatically readjust its size to the text entered.
4. Changing the x value does NOT change the width of the stage, in case for some people who asssumed the stage width was 0 to where ever the display object ended (x + width)
5. Adding another display object at 0,0 DOES change the stage width, since that object starts at x of 0, the total width is now 150.



References

livedocs, stage.stageWidth property definition
livedocs, stage.width property definition


none
none none none







History Licensing & Pricing Privacy Policy Contact Us
Frequently Asked Questions