3

In my document class named Engine, the stage variable is for some reason null:

package game
{

    import flash.display.MovieClip;
    import flash.display.Stage;
    import flash.events.Event;
    public class Engine extends MovieClip
    {
        public function Engine()
        {
            trace(stage); // gives Null
        }
    }
}

This was working fine up until now. I just recently added two dynamic text fields into a symbol, and all of a sudden the stage is null. I really don't see the connection.

This is my first time using actionscript and flash, so am a bit confused at everything.

Razor Storm
  • 12,167
  • 20
  • 88
  • 148
  • There's no reason that stage would ever yield null in the document class. Something else is up. Or you're trying to initialise the document class somewhere else maybe. Do you have more info on the symbol and the two dynamic text fields? – Marty May 13 '11 at 06:49

2 Answers2

6

Ok, I have reproduced what you have been experiencing, by adding a TLF font to the stage in CS5, that must be what's causing it. But this code should solve your problem :

public function Engine():void 
{ 
     if( !this.stage ) 
         this.addEventListener( Event.ADDED_TO_STAGE, init ); 
     else 
         init(); 
} 

private function init(e:Event = null):void 
{ 
   this.removeEventListener( Event.ADDED_TO_STAGE, init ); 
   trace(stage);
 }  

My guess is that there is some delay in creating the stage in this case. I've had this happen and didn't know why, so this was the way I handled it. It works! :)

prototypical
  • 6,731
  • 3
  • 24
  • 34
0

I just ran into this on a project that I have been working on for a while. All of a sudden my constructor in my document class was finding null for it's stage.

In my case (after hours of sleuthing) I guessed that an asset had gotten added to the FLA which had one TLF TextField in it. I guessed it because I saw the customary error once, but never again.

Normally this would cause an error that is quickly recognizable and easy to fix. Not this time. I found that my document class was getting added to the Stage and then immediately removed from the stage - at least the Event.REMOVED_FROM_STAGE event was firing.

The FLA has lots of assets in it and trying to track down one stray TLF TextField (which could be empty) was pretty daunting, so I saved the FLA as an AS2 project, then saved it back to an AS3 project. All my linkages were fine and the TLF TextField (wherever it was) was converted to a Classic one. All was well again.

Here is a post talking about the same fix for a more direct problem : How to stop/convert TLF textfield used in flash files when you have list to search?

Community
  • 1
  • 1
rushkeldon
  • 1,343
  • 1
  • 10
  • 13