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?