View Single Post
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator 17 Creative Assets 2008-03-20 #4 Old  
I would approach the singleton implementation in a slightly different way for this. You don't want to (and shouldn't) expose all of the Stage's properties and methods globally. For example, exposing addChild() and removeChild() ect wouldn't be a good idea.

ActionScript 3.0 Code:
  1. package
  2. {
  3.     import flash.display.Stage;
  4.  
  5.     public class GlobalStage
  6.     {
  7.         //----------------------------------------------------------------------
  8.         //
  9.         // Private Properties
  10.         //
  11.         //----------------------------------------------------------------------
  12.  
  13.         private static var instance:GlobalStage = null;
  14.  
  15.         private static var stage:Stage = null;
  16.        
  17.         //----------------------------------------------------------------------
  18.         //
  19.         // Constructor
  20.         //
  21.         //----------------------------------------------------------------------
  22.        
  23.         public function GlobalStage( stageRef:Stage )
  24.         {
  25.             if( instance != null )
  26.             {
  27.                 throw new Error( "..." );
  28.             }
  29.  
  30.             instance = this;
  31.             stage = stageRef;
  32.         }
  33.        
  34.         //----------------------------------------------------------------------
  35.         //
  36.         // Getters/Setters
  37.         //
  38.         //----------------------------------------------------------------------
  39.  
  40.         public static function get align():String
  41.         {
  42.             return stage.align;
  43.         }
  44.  
  45.         public static function set align( value:String ):void
  46.         {
  47.             stage.align = value;
  48.         }
  49.  
  50.         public static function get scaleMode():String
  51.         {
  52.             return stage.scaleMode;
  53.         }
  54.  
  55.         public static function set scaleMode( value:String ):void
  56.         {
  57.             stage.scaleMode = value;
  58.         }
  59.  
  60.     }
  61. }
At the very start of your code, when your app is initialised, you would initialise GlobalStage like this:

ActionScript 3.0 Code:
  1. import GlobalStage;
  2.  
  3. ...
  4.  
  5. new GlobalStage( stage );
Note that the GlobalStage instance isn't being assigned to a variable there. Any further attempts to initialise the class will throw a runtime Error.

You can then import GlobalStage in any other class you need to use it in:

ActionScript 3.0 Code:
  1. import GlobalStage;
  2.  
  3. ...
  4.  
  5. GlobalStage.align = "topLeft";
  6. GlobalStage.scaleMode = "noScale";
Although, I'm wondering why you would actually need to do that because you would setup the stage when the app is initialised, it doesn't make any sense to me allowing the Stage.align and Stage.scaleMode properties to be available globally like that ( it is obviously unavoidable if you access the stage property of a DisplayObject though ).
Reply With Quote