Friday, 2 October 2009

Stage Height and Width in Actionscript 3

The code for the stage height and stage width in Actionscript 3 has slightly changed from the Actionscript 2.0 version. Below is a comparison of the two versions.

AS2 code

stage.height

AS3 code
stage.stageHeight
As you can see the code looks very similar - only an extra word is added. However, using the wrong code can have a big impact on what you want to achieve in Actionscript 3. Below is a diagram showing the difference between the two variations of code.



To clarify the difference, the code ‘stage.height’ will only return the contents on the stage. For example, if your object on the stage has a height of 200 pixels then the 'stage.Height' will be 200. While the code ‘stage.stageHeight’ will give you the actual height of the stage. So, if you stage height is 400 pixels then the 'stage.stageHeight' will also be 400 pixels. The is exactly the same for the stage width as well.

Here is a quick example. My stage dimension are 550 x 400 pixels. I have drawn a box shape using the AS3 drawing API. The box shape's width and height are 200 x 200 pixels.

var box:Shape = new Shape();
box.graphics.beginFill(0x000000);
box.graphics.drawRect(0,0,200,200);
box.graphics.endFill();
addChild(box);

trace('stage.height is: ', stage.height);
trace('stage.width is: ', stage.width);
trace('stage.stageHeight is: ', stage.stageHeight);
trace('stage.stageWidth is: ', stage.stageWidth);

The following messages gets traced out. As you can see ''stage.height' returns the contents of the stage (the height of the box shape), and 'stage.stageHeight' returns the real stage height.

stage.height is:  200
stage.width is:  200
stage.stageHeight is:  400
stage.stageWidth is:  550

1 comments:

Luciano 22 October 2009 at 12:17  

thanks a lot!
very simple, very usefull...

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP