Ultrashock Forums > Flash > ActionScript
[AS3] Global Stage Singleton

You are currently viewing our website as a guest which gives you limited access to forums, files and other resources.

Click here to join now for free, and start interacting with our members, download files and much more!

Click here if you are looking for our Flash files and other professional assets.
 
Post Reply | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes

#1
Bookmark and Share!
[AS3] Global Stage Singleton
Old 2008-03-19

I need help/opinion on something. I built a GlobalStage class which is a singleton that allows me to have a global stage variable. I have tried a few ways and maybe its due to my lack knowledge working with singletons. But is it possible to have a method inside the singleton to handle the stageAlign, scaleMode and the other stage properties?

At the bottom is a piece of my class, but I would like to have this singleton control all the stage properties in addition to allow me to have a global stage variable.

Anytime I trace "stage" inside the singleton it comes up null? I don't have to use a singleton but I felt it would be appropriate for what I was trying to accomplish.

ActionScript Code:
  1. public static function get instance():GlobalStage
  2. {
  3.     if( _instance == null )
  4.     {
  5.         constructorLocked = false;
  6.         _instance = new GlobalStage();
  7.         constructorLocked = true;
  8.     }
  9.    
  10.     return _instance;
  11. }
  12.  
  13. public function get stage():Stage
  14. {
  15.     return _stage;
  16. }
  17.  
  18. public function set stage( value:Stage ):void
  19. {
  20.     if( value != null )
  21.     {
  22.         _stage = value;
  23.     }
  24.     else
  25.     {
  26.         value = null;
  27.     }
  28.    
  29.     _stage = value;
  30. }
I'm going to eat your brain and gain your knowledge.
postbit arrow 6 comments | 2118 views postbit arrow Reply: with Quote   
Registered User
Isocase is offline
seperator
Posts: 1,441
2006-11-03
Age: 28
Isocase lives in United States
Isocase's Avatar
seperator

Ultrashock Member Comments:
Azzan Azzan is offline Azzan lives in Singapore Creative Assets 2008-03-19 #2 Old  
you can read more about this here.
http://flashrevolution.net/_global-in-actionscript-3/
Reply With Quote  
Isocase's Avatar Isocase Isocase is offline Isocase lives in United States 2008-03-20 #3 Old  
Thanks Azzan for the link but not quite what I was looking for. With my global Singleton I can do something like

ActionScript Code:
  1. Global.instance.stage.align = StageAlign.TOP_LEFT;

Since that is stage functionality I wanted to be able to put that into my stage singleton. Thinking about it, it might not be the right way to go since my global singleton's only purpose pretty much is to create one instance. I was thinking of just making a stage manager class where I can handle all the stage properties and what not.
Reply With Quote  
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 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  
Isocase's Avatar Isocase Isocase is offline Isocase lives in United States 2008-03-20 #5 Old  
Thanks Nutrox your right I don't want stageAlign and stageScaleMode to be available globally. I would like to be able to setup those properties inside the GlobalStage singleton and not have to worry about it, but then I was wondering if setting up a separate StageManager class would be the way to go but that might just be overkill.
Reply With Quote  
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2008-03-20 #6 Old  
Why don't you just setup the stage in the main/document class?
Reply With Quote  
Isocase's Avatar Isocase Isocase is offline Isocase lives in United States 2008-03-20 #7 Old  
Yeah I think I will just setup the stage in the main/document class. Would it be a good idea to still keep my above singleton as a global stage reference or is there a better way to approach this?
Reply With Quote  
Thread Tools
Display Modes Rate This Thread
Rate This Thread: