Search code examples
actionscript-3classstaticscopestage

AS3 referancing the stage from a static class


I need to access the stage in a static class, the solution i found was to pass in the stage as an argument and to use that in my class's functions, but that seems sort of ugly, is there a better way of accessing the stage from a static class without anything needed in Main?


Solution

  • Stage hasn't static access to its instance so you always have to organize it yourself (flex3 for example has similar logic with SystemManager and Application instance). It's a normal implementation to have a public static property stage in class that required it (for example it can be Tooltip or CursorManager) and set this property in application initialization process because flash application always has stage:

    Main.as:
    
    private function init():void
    {
        Tooltip.init(stage);
        CursorManager.init(stage);
        ...
    }
    

    It's a low coupling solution where you use injection for the stage property.