Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 是否在stage/root上获取显示对象?_Actionscript 3 - Fatal编程技术网

Actionscript 3 是否在stage/root上获取显示对象?

Actionscript 3 是否在stage/root上获取显示对象?,actionscript-3,Actionscript 3,如何从类中获取stage/root上的显示对象 var txt = new TextField(); with(txt){ type = TextFieldType.INPUT; border = true; textColor = 0xffffff; multiline = true; x = 20; y = 20; width = 270; height = 40; } addChild(txt); txt.name = 'te

如何从类中获取stage/root上的显示对象

var txt = new TextField();
with(txt){
    type = TextFieldType.INPUT;
    border = true;
    textColor = 0xffffff;
    multiline = true;
    x = 20;
    y = 20;
    width = 270;
    height = 40;
}
addChild(txt);
txt.name = 'test';

class classTest {
    public function classTest{
        trace(this.getChildByName('test'));
    }
}
var cls = new classTest();
文本字段txt在根目录中,但如何从类中获取它

var txt = new TextField();
with(txt){
    type = TextFieldType.INPUT;
    border = true;
    textColor = 0xffffff;
    multiline = true;
    x = 20;
    y = 20;
    width = 270;
    height = 40;
}
addChild(txt);
txt.name = 'test';

class classTest {
    public function classTest{
        trace(this.getChildByName('test'));
    }
}
var cls = new classTest();

不知道我是否理解你的意思。 但是,如果向显示对象添加名称,则可以尝试下一个代码

txt.name = "txt_1";
this.getChildByName("txt_1");
更新

最好不要这样做。最好使用事件在类之间进行通信,但无论如何:

class classTest {
    private var _r : MovieClip; //not sure about the type
    public function classTest(r:MovieClip){
        _r = r;
        trace(_r.getChildByName('test'));
    }
}
var cls = new classTest(this);
(在新类文件中)


Event.ADDED_TO_STAGE
将允许您在启动该阶段时引用该阶段,如果您已实例化该类并将其添加到该阶段(这就是事件首先启动的原因)。

不确定您在做什么,但最简单的方法是解析对root或包含Textfield对象的DisplayObject对象的引用,然后通过ClassTest类中的该引用访问Textfield对象

var txt:TextField = new TextField();

with(txt)
{
     type = TextFieldType.INPUT;     
     border = true;
     textColor = 0xffffff;
     multiline = true;     
     x = 20;     
     y = 20;     
     width = 270;     
     height = 40; 
} 

addChild(txt); 
txt.name = 'test';  

class ClassTest 
{
     public function ClassTest(p_target:DisplayObjectContainer)
     {         
          trace(p_target.getChildByName("test"));  

     } // end function

}// end function

var classText:ClassText = new ClassTest(this); // parse a reference to root

好啊但是如果作用域是一个类,那么它就是类。。我如何引用stage/root范围?如果您使用像addChild(txt)这样的代码;然后可以使用getChildByName(“txt_1”)之类的代码;它将指的是舞台上的孩子们。试试这个。我如何从类内部引用根?这不是最好的选择,但可以使用:您可以创建一个引用根的变量。。我将更新答案您是否通过timeline或document类实现此代码?尝试调用getChildByName()将引发编译器错误,因为classTest类没有该方法。classTest需要对Sprite或MovieClip类进行子类化,以继承getChildByName()。