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.
[as]public static function get instance():GlobalStage
{
if( _instance == null )
{
constructorLocked = false;
_instance = new GlobalStage();
constructorLocked = true;
}
return _instance;
}
public function get stage():Stage
{
return _stage;
}
public function set stage( value:Stage ):void
{
if( value != null )
{
_stage = value;
}
else
{
value = null;
}
_stage = value;
}[/as]
you can read more about this here.
http://flashrevolution.net/_global-in-actionscript-3/
- 19 March 2008 11:52 PM
-
Thanks Azzan for the link but not quite what I was looking for. With my global Singleton I can do something like
[as]Global.instance.stage.align = StageAlign.TOP_LEFT;[/as]
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.
- 20 March 2008 07:34 AM
-
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.
[highlight=ActionScript 3.0]package
{
import flash.display.Stage;
public class GlobalStage
{
//———————————————————————————————————
//
// Private Properties
//
//———————————————————————————————————
private static var instance:GlobalStage = null;
private static var stage:Stage = null;
//———————————————————————————————————
//
// Constructor
//
//———————————————————————————————————
public function GlobalStage( stageRef:Stage )
{
if( instance != null )
{
throw new Error( “...” );
}
instance = this;
stage = stageRef;
}
//———————————————————————————————————
//
// Getters/Setters
//
//———————————————————————————————————
public static function get align():String
{
return stage.align;
}
public static function set align( value:String ):void
{
stage.align = value;
}
public static function get scaleMode():String
{
return stage.scaleMode;
}
public static function set scaleMode( value:String ):void
{
stage.scaleMode = value;
}
}
}[/highlight]
At the very start of your code, when your app is initialised, you would initialise GlobalStage like this:
[highlight=ActionScript 3.0]import GlobalStage;
...
new GlobalStage( stage );[/highlight]
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:
[highlight=ActionScript 3.0]import GlobalStage;
...
GlobalStage.align = “topLeft”;
GlobalStage.scaleMode = “noScale”;[/highlight]
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 ).
- 20 March 2008 12:18 PM
-
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.
- 20 March 2008 02:10 PM
-
- Log in or join for free to make a comment.


