When can you access a stage instance?
I'm using Flash CS5. I have some instances on my stage declared in the main class and I want to attach some events to them, however at the time of the main class constructor, these are declared as null.
What's the best practice for a开发者_JAVA技巧ccessing stage instances? Is there an event listener I could add that will tell me when the stage instance properties have been populated?
Simple example:
public class bleepBloop extends MovieClip {
public var productName:TLFTextField;
public function bleepBloop() {
trace( productName ); // Here it is null
}
}
However, when I access productName later, it's defined.
Edit: I answered a different question... Sorry about that! Hopefully this answers your actual question.
The stage instances inside a MovieClip should be initialized once you jump to the frame that actually contains that child:
trace(productName); // will trace null if productName isn't on the current frame
gotoAndStop(frameWithProductName);
trace(productName); // should now be initialized (when publishing for Flash Player 10+)
If the instances are on frame 1, they should be initialized and accessible right away in the constructor. Otherwise, one solution would be to place some timeline code that adds your desired listeners on the frame where the instances appear. Note that these instances are actually recreated when they disappear and reappear (when you leave and reenter that frame), so you have to readd your listeners in this case. Placing the listener code on that frame will take care of this.
Alternatively, you could listen for when the ADDED_TO_STAGE event fires with the particular object you are interested in:
public function bleepBloop() {
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage, true); // true to listen for children (capture phase)
}
private function onAddedToStage(event:Event):void {
if(event.target == productName) {
// add listeners
}
}
精彩评论