Jump to content
  • Advertisement
Sign in to follow this  
bepawuca

Getting the Stage Dimensions in AS3

This topic is 3174 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm beginning to learn AS3, so my experience with this a bit limited. Please excuse my stupidity. In AS3 I want to draw an image of Earth at the centered bottom of the screen. Here's my Earth.as file:
package  
{
	
	/**
	 * ...
	 * @author Me
	 */
	import flash.display.Scene;

	import flash.display.Sprite;
	import flash.display.Bitmap;
	import flash.display.Stage;

	 
	public class Earth extends Sprite
	{
		[Embed(source='resources/Earth.png')]
		private var earthSpriteClass:Class;
		private var earthSprite:Bitmap;
		public function Earth() 
		{
			earthSprite = new earthSpriteClass();
			addChild(earthSprite);
			earthSprite.x = stage.width / 2 - earthSprite.width / 2;
			earthSprite.y = stage.height - earthSprite.height / 2;
		}
		
	}
	
}

Now, I'm obviously doing something really stupid here. Compile is fine, but when I run my innocent-looking program, the Flash debugger cruelly lashes out at me with a dreaded #1009. TypeError: Error #1009: Cannot access a property or method of a null object reference. at Earth()[...\Earth.as:24] at Main/init()[...\Main.as:28] at flash.display::DisplayObjectContainer/addChild() at Preloader/startup()[...\Preloader.as:43] at Preloader/checkFrame()[...\Preloader.as:33]

Share this post


Link to post
Share on other sites
Advertisement
flash.display.Stage has stageHeight and stageWidth variables

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/Stage.html#stageHeight

Share this post


Link to post
Share on other sites
I've put the code you posted in the main class and it works. I dont know how's your main class, anyway the stage is null when the displayobject isnt on the stage yet.

Share this post


Link to post
Share on other sites
Quote:
Original post by fnm
I've put the code you posted in the main class and it works.


Yeah, doing it in Main works fine for me too. The problem is only when stage is referenced in a class other than Main.

Share this post


Link to post
Share on other sites
var e:Earth = new Earth();
addChild(e);
e.x = stage.width / [2] - e.width / [2];
e.y = stage.height - e.height / [2];



Something like this should work.

Share this post


Link to post
Share on other sites
Listen for the Event.ADDED_TO_STAGE event in the Earth constructor, and modify the sprite position in the event listener, then. While the sprite is not on the stage, his stage property is null.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!