Hit a major brick wall, stage is null

Hi…
I was just playng around with AS3 you know, wanting to make a website and all. Going pretty well. Learning all the new wonderful properties it has over AS2 and the new thinking and all. Then BAM!!!
Somone threw a brick in my face.
Want to access the stage class from a class that is not yet added to the displayobject. Ok, so I define my instance, then add it to the displayobject with addchild(). But between these two lines of code it runs the init function of my instance which looks a bit like:


public class ProjectMenu extends Sprite {
 var sW:Number;
 function ProjectMenu() {
   stageUpdate(null);
     addEventListener(Event.ENTER_FRAME,mouseCheck);
     stage.addEventListener(Event.RESIZE,stageUpdate);
   }
 function stageUpdate(e:Event) {
  sW=stage.stageWidth;
 }
}

So when I run my script I get this wopping error:

Error #1009: Cannot access a property or method of a null object reference.
    at scr::ProjectMenu/scr::stageUpdate()

Basically I worked it out to be that its trying to access the stage object without being added to the displayobject lists (which it can’t bloody well do!!!). This is ABSURD as it goes against the whole point of why im using this new language. I wanted greater control over where I could put my listeners instead of having to do it through loads of complicated things. The only work arounds I can see are:

  • Put the listener on the main class to call the stageUpdate in the instance of ProjectMenu (really complicated due to other factors in the script)
  • OR -
  • add ProjectMenu to the displayObject before running the init script
  • OR -
  • make a listener that listens when it is added to the displayobject to then make another listener for the stageUpdate

I’m not doing the first one. The second one is not possible. And after writing all of this, the third one is the solution. So in effect I have wasted all these lines of type and fidly formating to come up with the solution I was going to find anyway because of how I want my site to work.
Thanks kirupa and community,
Dan :smiley: