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
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
}
}
}
If that is your document class, then there is a good chance that this code is initializing before the stage object exists. I'm only guessing, but I expect that the change in behaviour is caused by your 'export for actionscript' classes loading their definitions on the first frame and causing some form of delay.
Try waiting for stage to exist:
package com {
import flash.display.MovieClip;
public class Teste extends MovieClip {
public function Teste() {
if(stage) {
init();
} else {
addEventListener(Event.ADDED_TO_STAGE,init);
}
}
private function init(evt:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE,init);
//stage should now exist
trace(stage);
}
}
}
精彩评论