Access Stage from anywhere in Actionscript 3

Friday, February 13th, 2009 | Business | by

Learned about a promising library for Actionscript 3 today, Casalib.  There will be more information about the library, as I’m just starting to get to playing with it.

Something that has been pissing off recently, has been the lack of ability to easily reference the stage from any object in AS3.  I find myself building controllers to manage objects in the stage, and I have to play tricks with passing objects around.  Why I haven’t written a class to access the stage is beyond me.

In looking at the blog for Casalib, I saw a post Get Access to the Stage Wherever You Need It.  

I’ll quote the article below:

In ActionScript 2, it was possible to access the stage from anywhere, through the static Stage class. With ActionScript 3, we lost that ability, and only display objects were permitted access—and only once they had been added to the stage.

To help you get access wherever you need it, CASA Lib for AS3 has a utility class called StageReference in the org.casalib.utilpackage. It’s a static class that lets you keep track of the stage (or multiple stages if you’re developing an AIR app with multiple windows). Here’s how we recommend using it.

In the constructor of your main document class, just call this line of code:

StageReference.setStage(this.stage);

Now, throughout your entire site or application, you can get access to the stage reference from anywhere by using this line of code:

StageReference.getStage();

Take a look at the class’s documentation to see what else the StageReference class can do. Some classes (like CASA Lib’s Keyclass) require the use of the StageReference class to function properly.

Related Posts

Tags: , , , , , ,

7 Comments to Access Stage from anywhere in Actionscript 3

jack
July 23, 2009

So essentially the stage is accessed using the singleton pattern.

jack
July 23, 2009

Actually, no not really a singleton pattern. My bad. I should look at the actual thing before I comment.

seangw
July 23, 2009

Yeah, not quite a singleton pattern. More of restricted access. You have the option in the controlling class to allow all movies to call the stage through a universal class (StageReference) only if you define it.

I guess a Singleton pattern would mean there is only one stage (which there is) because it works better that way. There is, but it doesn’t really affect the method of access (there would only be 1 stage in any scenario … even if all objects could access it).

Thanks for pointing that out, it’s a good differentiation.

Aaron
January 4, 2010

Hi Sean,

Thanks for the link/post to CASA Lib. The reason the class is not setup as a singleton, is because when you are working in AIR there are could be multiple stages (different windows) and we built the class to be flexible to work in that environment as well.

Sharedtut
February 3, 2010

Good post, thank you for sharing.

Staging
June 23, 2010

s. if you want to access root. it should be added in to the stage means timeline. so check the trace after the class added to stage.

al77ex
February 18, 2011

In my case StageReference works only in flashplayer 9 env. If I publish swf with FP10,
setStage gives Null

Leave a comment

*


Please share, it makes me happy:

Subscribe to Email Alerts

Follow Me

Follow seangw on twitter

Archives

Categories

prestashop theme

virtuemart template