Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 214
Razor Storm Avatar asked Feb 02 '26 19:02

Razor Storm


1 Answers

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

like image 96
prototypical Avatar answered Feb 04 '26 14:02

prototypical



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!