Richard Parnaby-King

Web Developer – PHP, Zend Framework and Actionscript 3

Posted on | | No Comments

Positioning elements on screen is a tricky process when you are doing it only by code. And even harder when you want to make the class you are writing modular or reusable. A very simple example being, you want to set the width and height of the stage to 800 x 600, then import a preloader that places some loading text at the very centre of the stage.

This post will show how to access the width and height dimensions of the stage in the preloader class.

First of all, to set the dimensions of the stage, in our main.as file, add:
[as3]
[SWF(width=’800′,height=’600′,backgroundColor=’#ffffff’,frameRate=’25’)]
[/as3]
If you are using Flash then access the stage properties panel and change the dimensions there.

Now we know what the stage dimensions are, we need to access them. Unless you are aware of the correct stage properties your instinct may be to use stage.width and stage.height, however this will only give the width and height of the current object, not the stage object.

The Solution

What you want to use are stage.stageWidth and stage.stageHeight.

In our subclass:
[as3]
//see if we have access to the stage yet. If not, wait till we do.
public function Preloader():void {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}

//add event listeners, instantiate properties, etc
private function init():void {
//… instantiate textfield with our loading message in it
//… add progress event listener
addChild(myTextfield);

myTextfield.x = (stage.stageWidth / 2) – (this.width / 2);
myTextfield.y = (stage.stageHeight / 2) – (this.height / 2);
}
[/as3]

Download the as3 preloader class.

Conclusions

We can access the width and height of the stage by using the stage.stageWidth and stage.stageHeight properties.

Posted By:

Comments

  • ABOUT

    Having fifteen years of programming experience in PHP, Zend Framework and ActionScript3, I have a very strong working knowledge of object orientated programming.

    I am a PC Gamer! Playing FPS, RTS, RPG and the occasional MMO since 1996 I have a huge number of a variety of fast-paced games.

  • Recent Posts

  • Categories

  • RSS SUBSCRIBE TO OUR FEED