Razor Storm
Razor Storm

Reputation: 12336

stage is null in document class? ActionScript 3.0 Flash CS5

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.

Upvotes: 3

Views: 1968

Answers (2)

rushkeldon
rushkeldon

Reputation: 1391

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?

Upvotes: 0

prototypical
prototypical

Reputation: 6751

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! :)

Upvotes: 6

Related Questions