2011-10-14 70 views
0

我有一个外部的AS3类文件,它被加载到Flash电影的第一帧。如何引用外部动作文件中的舞台对象?

如何在AS3文件中引用舞台对象而不必将其作为参数传递?我的意思是我觉得舞台对象在全局领域 - 或者我对这个假设不正确?

回答

0

Stage是'stageable'对象的属性:从DisplayObject派生的每个对象都有权访问stage:Stage属性。

所以,Movieclips和Bitmaps可以通过它们的祖先访问舞台属性。

“自动”设置对象的stage属性的方法是通过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 
    } 
} 
+0

轻微更正:如果其中一个祖先实际位于显示列表中,则DisplayObjects可以通过它们的祖先访问舞台。这里的要点是Stage不是全球性的 - 例如,在多窗口AIR应用程序中,每个窗口都可以有自己的舞台。所以'DisplayObject.stage'不会引用** stage,如果你连接到一个stage,它会引用** a ** stage,否则就不会有任何内容。 – fenomas