How to reference the stage object in external actionscript files?
I have an external AS3 class file which is loaded up on the first frame of the Flash movie.
How can I reference the stage object in the AS3 file without having to pass it as a parameter?I mean it seems to me like the stage object is in th开发者_运维百科e global realm - or am I incorrect with this assumption?
Stage is a property of 'stageable' objects: each object derived from DisplayObject, has access to stage:Stage
property.
So, Movieclips and Bitmaps have access to the stage property through their ancestor.
A way to "automatically" set the stage property of an object is to add the objects to the display list via addChild().
var mc:MovieClip = new MovieClip();
mc.addEventListener(Event.ADDED_TO_STAGE, func);
trace(mc.stage); //null
addChild(mc);
function func(e:Event){
mc.stage; //defined, returns reference to the parent since we added it to the display list
}
//this is how to use the listener inside the class
public class Grr extends MovieClip{
public function Grr(){
this.addEventListener(Event.ADDED_TO_STAGE, checkF);
}
public function checkF(e:Event){
//inside this function I can do whatever I want that requires stage
}
}
精彩评论