Accessing the Stage From an External Movie Clip

Lots of things have changed in the migration from AS2 to AS3. One of which is the stage and how it is accessed. First, you need to understand that there is only one stage. Therefore, in the example I am going to give the external movie clip doesn’t have its own stage. The stage is a Display Object Container and houses all other display objects including the main timeline.

Having access to the stage and its properties is a very useful thing. I find it helps me be a little more dynamic in my coding and not be restricted to hard coded variables. Quite often I find myself centering both vertically and horizontally with respect to the stage. Being able to access properties such as stageHeight and stageWidth allow me some flexibility were I to ever change the size of the stage.

An Example

When I’m loading large Flash apps I usually start with a “shim” swf, which has the preloader only. This reduces the time of the very first initial load. That shim then preloads and loads the main site, or whatever it is. Other people may preload differently, but this is what I’m accustomed to. Here’s a visual:

Example 1

The Problem

Simply put…It’s not straight forward how you access the properties of the “Shim” stage from the “Main App” once you’ve loaded it in. If you’ve run into the problem you know what I’m talking about and are probably really tired of trying to figure out why the stage is null. Many people will tell you that this is a bug in Flash, while other people just curse. In all reality, you just need a little forethought on the part of the developer and things turn out fine.

I’ve seen an example from the all mighty Senocular suggesting that you could create a “TopLevel” class and then extend your subsequent classes off that one. But, as he mentions, this is not the best solution if you happen to be working in a group and files are being passed around. It is very likely that the others might not have your TopLevel class at which point things go to crap.

The Solution

In my example I simply place the following code in the constructor function of my externally loaded swf:

if (stage == null) {

	addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

} else {

	onAddedToStage();

}

The above code obviously calls a function, onAddedToStage(), which can kick things off for you and the rest of your app. Within this function you should place anything that might need to reference stage properties and the like.

I’d be curious as to what others like doing. This is my preferred method as it seems quick and straight forward.

This entry was posted in Flash/ActionScript, Web Development and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">