Actionscript 3 从secondary.as访问.fla元素

Actionscript 3 从secondary.as访问.fla元素,actionscript-3,flash,Actionscript 3,Flash,我想从其他.as而不是主类访问.fla的元素(如按钮、文本字段等) 编辑:我将分享更多信息的代码 我有一个gallery.fla,其中的文档类是gallery.as。代码并不重要,在这里我读取了一个XML,取决于某个标记的值,我希望另一个类修改gallery.fla上按钮的标签 画廊 package{ import otherClass; public class gallery() extends MovieClip{ private var otherGallery:otherCl

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

编辑:我将分享更多信息的代码

我有一个gallery.fla,其中的文档类是gallery.as。代码并不重要,在这里我读取了一个XML,取决于某个标记的值,我希望另一个类修改gallery.fla上按钮的标签

画廊

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

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

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

这是范围问题。AS3中的所有对象都是类,而在编程中,变量和实例仅在其范围内可用

有几种方法可以让主时间线的范围从它的一个子节点获得参考

1。简单的马虎方式

您可以使用
root
关键字获得对主时间线/文档类的引用

MovieClip(root).btnOpen.label = "labelChanged";
将类添加到显示后,根变量变为可用

2。将对主时间线/文档类的引用传递给其他类

因此,在Main.as(或Main timeline,或document类)中,您可以执行以下操作:

myGalleryInstance.main = this; 
//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";
    }
} 
然后您的
多媒体资料
类将如下所示:

myGalleryInstance.main = this; 
//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";
    }
} 

根目录
类似,
父目录
仅在将对象添加到显示器后才可用

您需要精确地确定组件的层次结构。答案还取决于“external.as”如何出现在已发布的Flash应用程序中。你真的应该尝试一些东西。让我们看看,我会更具体。我有一个带有按钮和其他东西的.fla,这取决于我使用.as或另一个.as的某些条件,但我无法从那里访问元素的属性。你需要尝试一些东西并共享代码,然后只有我们可以帮助你。嗯,我试过这3个选项,第一个对我来说很好。谢谢