00:00
00:00
Newgrounds Background Image Theme

Ryor just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Heros Constructer cant access stage

553 Views | 4 Replies
New Topic Respond to this Topic

In my document class I add a hero movieclip to the stage.

package 
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flash.display.Stage;

	public class AlternativeRPG extends MovieClip
	{
		public static var hero:Hero = new Hero();
		public static var stageWidthContainer:int;
		public static var stageHeightContainer:int;
		public function AlternativeRPG()
		{
			addEventListener(Event.ENTER_FRAME,gameUpdate);
			stage.addEventListener(KeyboardEvent.KEY_UP,keyUpChecker);
			stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownChecker);
			stageWidthContainer = stage.stageWidth;
			stageHeightContainer = stage.stageHeight;
			gameInit();
		}
		public function gameInit()
		{
			addChild(hero);
			trace(stageWidthContainer);

		}
	}
}

I use the public var stageWidthContainer so that I can access the stage width from my hero class. When i trace it out in the document class it works fine. but when I try to access it in the constructor function of my hero

package 
{
	import flash.display.MovieClip;

	public class Hero extends MovieClip
	{

		public function Hero()
		{
			placeHero();
		}

		private function placeHero()
		{
			x = AlternativeRPG.stageWidthContainer / 2;
			y = AlternativeRPG.stageHeightContainer / 2;
		}
	}
}

i get the error TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Hero/placeHero()
at Hero()
at AlternativeRPG$cinit()
at global$init()

Originating from the line trying to access stageWidthContainer. Why does this variable throw an error when it is called like this?


...

Response to Heros Constructer cant access stage 2012-05-29 04:36:18


Change to:

package 
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flash.display.Stage;

	public class AlternativeRPG extends MovieClip
	{
		public static var hero:Hero;
		public static var stageWidthContainer:int;
		public static var stageHeightContainer:int;
		public function AlternativeRPG()
		{
			addEventListener(Event.ENTER_FRAME,gameUpdate);
			stage.addEventListener(KeyboardEvent.KEY_UP,keyUpChecker);
			stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownChecker);
			stageWidthContainer = stage.stageWidth;
			stageHeightContainer = stage.stageHeight;
			gameInit();
		}
		public function gameInit()
		{
			hero = new Hero();
			addChild(hero);
			trace(stageWidthContainer);

		}
	}
}

The constructor of hero is called before the properties are being set... you should never instantiate vital objects like that. Instead create them in your constructor where the order is clear and bugs are easier to see.

Response to Heros Constructer cant access stage 2012-05-29 04:37:18


Ignore last post stupid logic error as a result of late night =)


...

Response to Heros Constructer cant access stage 2012-05-29 04:41:12


The constructor of hero is called before the properties are being set... you should never instantiate vital objects like that. Instead create them in your constructor where the order is clear and bugs are easier to see.

Yup that was the problem, I shouldve spotted that earlier. Silly mistake thanks for the help =)


...

Response to Heros Constructer cant access stage 2012-05-29 07:04:41


At 5/29/12 04:15 AM, the1manwiththeplan wrote: I use the public var stageWidthContainer so that I can access the stage width from my hero class.

Which is pointless as the stage is available within the Hero class as soon as its object is added t the stage.

package 
{
	import flash.display.MovieClip;

	public class Hero extends MovieClip
	{

		public function Hero()
		{
			placeHero();
                        addEventListener(Event.ADDED_TO_STAGE, onStage);
		}

		private function onStage(e:Event)
		{
			x = stage.stageWidth / 2;
			y = stage.stageHeight / 2;
		}
	}
}

I would not hard code the position into the Hero class like that, rather set it from the document class.

Using static variables like you do is really horrible, please stop it.