May 20, 2010

ACCESSING THE STAGE FROM A CLASS IN AS3

Here's a quick way (that works) of accessing the stage from a class in AS3. There are other methods. I don't see why this won't work, though.

The only difference here is that I'm waiting until the object has been added to the stage before trying to access any of the stage's values.

package
{

	import flash.display.*;
	import flash.events.*;

	public class MyClass extends Sprite
	{
		
		public function MyClass()
		{
			this.addEventListener(Event.ADDED_TO_STAGE, getStage);
		}
		
		private function getStage(e:Event):void
		{
			trace(stage.stageHeight + " : " + stage.stageWidth);
		}
	
	} //ends class
	
} //ends package
Posted in actionscript

What do you think?

(All fields required. Only your name and comment will be stored.)
Name:

Email:

Code:
validation image
Message: