Skip to content


Getting stageWidth and stageHeight

To expand on yesterday’s post, I added a few more lines of code to demonstrate how to retrieve the stage’s dimensions with the stage.stageWidth and stage.stageHeight methods.. You can see the edits in the SubClass.as file.

The Main.as file:

package {
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.DisplayObject;
	import fl.controls.TextArea;
	import fl.controls.ScrollPolicy;

	public class Main extends Sprite
	{
		// claim the stage
		public static var stage:Stage;
        public static var root:DisplayObject;

		//text area component
		private var output:TextArea;

		public function Main()
		{
			// build the UI
			ShowOutput();

			// setup stage
			Main.stage = this.stage;
			Main.root = this;

			// add the class
			var MC:MainClass = new MainClass();
			addChild ( MC );
		}

		public function traceOut():void
		{
			Message ( "This function is called from the SubClass.as file" );
		}

		/*
		FOR DISPLAY OF OUTPUT
		============================================================
		*/
		private function ShowOutput():void
        {
			output = new TextArea();
			output.verticalScrollPolicy = ScrollPolicy.ON;
			output.condenseWhite = true;
			output.setSize(stage.stageWidth, 150);
			output.move(0, 0);
			addChild(output);
        }

		public function Message(msg:String):void
		{
			output.appendText ( "\n" + msg );
		}
	}
}

The MainClass.as file:

package
{
	import flash.display.Sprite;

	public class MainClass extends Sprite
	{
		public function MainClass()
		{
			var SC:SubClass = new SubClass;
			addChild ( SC );
		}
	}
}

The SubClass.as file:

package
{
	import flash.display.Sprite;

	public class SubClass extends Sprite
	{
		public function SubClass()
		{
			// Call the function in the Main Document Class
			Main(Main.root).Message ( "This is the value if you use Main(root) = " + Main(root) );
			Main(Main.root).traceOut ();

			// Get the Stage Width
			Main(Main.root).Message ( "\n\nStage Width: "  + Main.stage.stageWidth );

			// Get the Stage Height
			Main(Main.root).Message ( "Stage Width: "  + Main.stage.stageHeight );

		}
	}
}

Posted in Actionscript 3.


One Response

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. eryx says

    Thank you for this post, made my life easier



Some HTML is OK

or, reply to this post via trackback.