Search code examples
flashnullflash-cs5stage

Stage null ActionScript 3 Flash CS5 Export for Actionscript


Here is the deal, I have a Main class that I call from the .fla file. Everything work fine until I set a object in the Library "export for actionscript"... after that the stage didn't work anymore, the stage now return "null", just because I checked an object to "export for actionscript".

I'm using flash cs5 and it never happen to me with the early version of flash.

Sorry about the english... and I hope I was clear enough.

Code Sample:

package com {

import flash.display.MovieClip;

public class Teste extends MovieClip {

    public function Teste() {
        trace(stage)// traced null 
    }
}
}

I'll try to explain the steps that I make. - Create that Teste Class; - Open the .fla file, and add that Class in the Class field on the Property of the .fla file; - create and MovieClip and try to export it from Actionscript; And here comes the problem... when I did it the stage now return Null, just because I exported a MovieClip in the Library. o.O


Solution

  • The reason is that Stage probably became available right away when there was not much to load. Now that you have added something to export for Actionscript the loading might even take a millisecond longer and stage wont be available.

    Always check if stage exists and then wait for it to exist before trying to reference it.

    Try this:

    package com {
    
    import flash.display.MovieClip;
    
    public class Teste extends MovieClip {
    
        public function Teste() {
            if(!stage) addEventListener(Event.ADDED_TO_STAGE, _addedToStage)
            else _addedToStage();
        }
    
        private function _addedToStage(e:Event = null)
        {
            trace(stage)// traced null 
        }
    }
    }