redconservatory
redconservatory

Reputation: 21934

Get the stage dimensions without using a Singleton?

I have a few classes in my project which aren't display objects, but they need to know about the stage of my project (stage.stageWidth, stage.stageHeight).

Is there a simple way to pass this information along to my classes without using a Singleton or passing these items in as parameters into the constructor??

Upvotes: 1

Views: 415

Answers (2)

Taurayi
Taurayi

Reputation: 3207

You can store a reference to stage in a class's static property which can be accessed from any class in your application. The following is an example of this:

GlobalVars.as:

package
{
    import flash.display.Stage;

    public class GlobalVars
    {
        public static var stage:Stage;

    }// end class

}// end package

Main.as:

package
{
    import flash.display.Sprite;

    public class Main extends Sprite
    {
        private var _stageAccessor:StageAccessor;

        public function Main():void 
        {
            init();

        }// end function

        public function init():void
        {
            GlobalVars.stage = stage;

            _stageAccessor = new StageAccessor();
            _stageAccessor.traceStageWidth(); // output: 
            _stageAccessor.traceStageHeight(); // output: 

        }// end function

    }// end class

}// end package

StageAccessor.as:

package
{
    import flash.display.Stage;

    public class StageAccessor
    {
        public function StageAccessor():void {};

        public function traceStageWidth():void
        {
            trace(GlobalVars.stage.stageWidth);

        }// end function

        public function traceStageHeight():void
        {
            trace(GlobalVars.stage.stageHeight);

        }// end function

    }// end class

}// end package

Upvotes: 1

Simon Eyraud
Simon Eyraud

Reputation: 2435

There is no way to do it without a singleton if you don't have a reference to a displayed graphic object.

Upvotes: 0

Related Questions