R – Access stage from the “main” class

actionscript-3mxmlc

I have the following ActionScript:

package {
    import flash.display.Sprite;

    public class Application extends Sprite {
        public function Application(){
            width=1000;
            height=500;
        }
    }
}

Which I compile with mxmlc Application.as. What I have noticed is that Application is not the stage object, like I thought it would be, because the width and height of the stage are not changing.

How do you access the stage from Application?

Best Answer

You're subtly misunderstanding what "Stage" refers to. The stage is the lowest-level reference to the display area Flash has to work with, so its size is ultimately dictated by the container Flash is executing in.

Thus when you view your content in the standalone Flash player, to resize the stage you resize the player itself, and when you view your content embedded in an HTML page, the stage only resizes when the browser changes the size of the element Flash is embedded into (via Javascript, for instance). Likewise if your flash was embedded into a .NET application, the .NET logic has control over the size of the stage, and so on.

For these reasons, it is not generally possible to resize the stage from within application logic, unless the container exposes a way to do it. Most browsers do indeed expose such functionality via JavaScript, so in a browser you can normally resize the stage by calling JS hooks to change the size of Flash's embed element. In contrast the standalone player exposes no such hooks, so resizing the stage is impossible (except of course that you can toggle fullscreen).

As a side note, as Joel Hooks points out, you can include a statement in your project of the form: [SWF(width=1000,height=500)]. This causes the compiled SWF to contain metadata for the stated size. That metadata is only a suggestion, however, and it's entirely up to the container whether to honor it or not. The standalone player will honor such metadata (for the initial container size), but browsers will ignore it entirely.

Related Topic