Be a Supporter!

Heros Constructer cant access stage

  • 261 Views
  • 4 Replies
New Topic Respond to this Topic
the1manwiththeplan
the1manwiththeplan
  • Member since: Jun. 10, 2008
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Heros Constructer cant access stage May. 29th, 2012 @ 04:15 AM Reply

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?


...

Sandremss128
Sandremss128
  • Member since: Aug. 22, 2009
  • Offline.
Forum Stats
Supporter
Level 11
Programmer
Response to Heros Constructer cant access stage May. 29th, 2012 @ 04:36 AM Reply

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.

the1manwiththeplan
the1manwiththeplan
  • Member since: Jun. 10, 2008
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to Heros Constructer cant access stage May. 29th, 2012 @ 04:37 AM Reply

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


...

the1manwiththeplan
the1manwiththeplan
  • Member since: Jun. 10, 2008
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to Heros Constructer cant access stage May. 29th, 2012 @ 04:41 AM Reply

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 =)


...

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to Heros Constructer cant access stage May. 29th, 2012 @ 07:04 AM Reply

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.