The Ultrashock Ultra Bundle
  • Home
  • Community
  • Forum
  • Flash
  • ActionScript
  • Thread
  •  
  • Previous topic
  • Next topic
Sign up to post

Flash
 ActionScript

  • Isocase Author 
    • 3366 
    • 0 
    • 6 
    [AS3] Global Stage Singleton
    Isocase

    Last reply Mar 20 2008, 03:00 PM

    by Isocase

    Posted: Mar 19 2008, 11:37 PM

    by Isocase

     

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]

  6 REPLIES
 
Azzan
1  
Azzan

you can read more about this here.
http://flashrevolution.net/_global-in-actionscript-3/

  • 19 March 2008 11:52 PM
  •  
Isocase Author 
2  
Isocase

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
  •  
Nutrox
3  
Nutrox

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
  •  
Isocase Author 
4  
Isocase

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
  •  
Nutrox
5  
Nutrox

Why don’t you just setup the stage in the main/document class?

  • 20 March 2008 02:20 PM
  •  
Isocase Author 
6  
Isocase

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?

  • 20 March 2008 03:00 PM
  •  
  •   Log in or join for free to make a comment.
 
Topic actions
  •  Share on Facebook
  •  Share on Twitter
Topic Categories
  •  Show All Topics
  •  Development
    •  Server Side
    •  Client Side
  •  Creative Software
    •  Web
    •  Video
    •  3D
    •  Illustrator
    •  Photoshop Battles
    •  Photoshop
  •  Design
    •  Typography
    •  Resources & Insight
    •  Checkpoint
  •  Career
    •  Copyright Matters
    •  Advice & issues
    •  Job Seekers
    •  Job Offers
  •  Flash
    •  UltraMath
    •  OOP
    •  Third Party Tools
    •  Open Source alternatives
    •  Data Communication
    •  Components
    •  Flex
    •  AIR
    •  Flash Lite
    •  Flash Professional
    •  Flash Newbie
    •  ActionScript
    •  XML
  •  Lounge
    •  Polls
    •  Random Chat
    •  Showcase And Critique
    •  BombShock Award Nominations
  •  Community Essentials
    •  BombShock Award Winners
    •  Tutorials
    •  Interviews
    •  News
    •  Bitmap tutorials
Popular Topics
  • Sort by: 
  • Activity
  • Views
  • Comments
  • Likes
Advertise with us
  • Your advertisement here!
  • loading
Ultrashock
  • Creative Assets
  • Community
  • Blog
  1. Home
  2. Forum
+/-
Creative Assets
  • Categories
  • Contributors
  • How to buy
Make Money
  • Commission Rates
  • Referral Program
  • Contributor Program
Community
  • Activity Feed
  • Forum
  • Profiles
About
  • Quick Tour
  • Our History
  • Banners & Logos
Support
  • Contact Ultrashock
  • Advertise with us
  • Legal Information
  •  Keep up to date
  • Flash 775  Flash
  • Audio 6,481  Audio
  • Vector 2,130  Vectors
  • Image 12,338  Images
  • Creative Assets 21,724  Assets
  • Profiles 282,659  Members
  • Topics 93,762  Topics
  • Blog 4  Blog
  • Facebook 1,680  Facebook
  • Twitter 1,165  Twitter
  • Join our FREE monthly newsletter!
  • Archive
  • Invalid email address. Please try again.
Subscribe
  • ©2012 Ultrashock LLC - All rights reserved
  • Terms of Use
  • Privacy Policy
  • Switch to dark theme
  • RSS Feeds
  • Top

©2012 Ultrashock LLC - All rights reserved

Printed on Sat, February 04, 2012 - 19:06:18