从辅助 .as 访问 .fla 的元素

Access to elements of .fla from secondary .as

我想从其他 .as 而不是主要的 .fla 访问元素(如按钮、文本字段等)class。

编辑:我将分享代码以获取更多信息。

我有一个 gallery.fla 文档 class 是 gallery.as。代码并不重要,我在那里读了一个 XML 并且取决于我想要另一个 class 的一些标签的值修改 gallery.fla

上的按钮标签

gallery.as

package{
import otherClass; 
public class gallery() extends MovieClip{
    private var otherGallery:otherClass = new otherClass();
    public function gallery(){
          if(x="xmlValue"){
             otherGallery.changeLabel();
          }     
     }
 }  
}

otherClass.as

public function changeLabel():void{
  btnOpen.label = "labelChanged";
}

btn打开它在 .fla 上的一个按钮,我无法从 otherClass.as 访问 .fla 的任何元素。错误 1120:访问未定义的 属性 btnOpen。​​

这是一个范围问题。 AS3 中的所有对象都是 classes,并且在编程中变量和实例仅在它们的范围内可用。

您可以通过几种方法从其中一个子时间轴获取对主时间轴范围的引用。

1.简单草率的方式

您可以使用 root 关键字获得对主要 timeline/document class 的引用。

MovieClip(root).btnOpen.label = "labelChanged";

根变量可用 class 已添加到显示中后。

2。将对主要 timeline/document class 的引用传递给您的其他 classes

因此,在您的 Main.as(或主时间线或文档 class)中,您可以这样做:

myGalleryInstance.main = this; 

那么您的 gallery class 将如下所示:

//those parenthesis don't belong in the class declaration --> () <--, I've taken them out
public class gallery extends MovieClip {
    private var otherGallery:otherClass; //better to instantiate complex objects in the constructor
    public var main:MovieClip; //create a public var you can populate

    public function gallery(){
          otherGallery = new otherClass();
          if(x="xmlValue"){
             otherGallery.changeLabel();
          }     
    }

    public function changeLabel():void{
        main.btnOpen.label = "labelChanged";
    }
} 

3。使用父关键字 - 草率的方式 #2

如果你galleryclass和otherCLass是显示对象,你可以使用parent关键字获取Mainclass/main时间线。因此,假设 gallery 是主时间轴的子级,而 otherClass 是 gallery 的子级,从 gallery.as 你会做:

//the main timeline is the grandparent of 'this' (otherClass)
MovieClip(MovieClip(parent).parent).btnOpen.label = "labelChanged";

root一样,parent仅在对象添加到显示后可用。