ActionScript3的:我怎么能访问外部类的舞台元素呢? [英] Actionscript3: how can I access elements on the stage from external classes?

查看:146
本文介绍了ActionScript3的:我怎么能访问外部类的舞台元素呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个外部类中的一个。至于文件,我的问题是,我无法访问的元素在舞台上。 code像stage.txtfield.text或this.parent.txtfield.text不起作用。该txtfield是一个动态文本字段的instace名称。

I have an external class in a .as file, my problem is that I can't access elements on the stage. Code like stage.txtfield.text or this.parent.txtfield.text does not work. The txtfield is the instace name of a Dynamic Text field.

推荐答案

这取决于位外部类。

如果它延伸的DisplayObject(或的DisplayObject的任何孙子),你将与道具能够访问,只要它被添加到显示列表(当它添加到舞台或屏幕上任何其他的DisplayObjectContainer这是列表)。

If it extends DisplayObject (or any grandchild of DisplayObject), you will be able access with the stage property as soon as it is added to the display list (which is when it's added to the stage or any other DisplayObjectContainer on the display list).

要监听使用下面的code。在外部类:

To listen for that use the following code in the external class:

addEventListener(Event.ADDED_TO_STAGE, AddedToStage);

//...

private function AddedToStage(e:Event):void
{
    trace("TextField text" + TextField(stage["textfield"]).text);
}

如果它不是一个DisplayObject,或者如果它不会是显示列表中,最好的事情将proberly是给它,它需要访问(如文本字段)的对象,无论是在构造器或一个单独的方法调用。 你可以给它一个参考阶段,它的自我,但例如,如果你需要的类来操作的MovieClip一个TextField这不会是很普通的。

If it not a displayObject or if it's not gonna be on the display list, the best thing would proberly be to give it the objects that it needs to access (like the TextField), in either the contructor or a seperate method call. You could give it a reference to the stage it self, but that wouldn't be very generic if for example you need the class to manipulate a TextField inside MovieClip.

您可以给在对与此code中的文本字段:

You could give at reference to the TextField with this code:

//In any DisplayObject on the display list (could be inside a MovieClip or on the Stage itself)

var manipulator:MyClass = new MyClass(TextField(stage["textfield"]));

//In the external class

public class MyClass
{
    publich function MyClass(txt:TextField)
    {
        trace("TextField text" + txt.text);
    }
}

你要知道,这code不检查文本框实际上是在那里。你应该检查第一和抛出一个正确的错误,使调试更加容易。

Mind you that this code doesn't check if the textfield is actually there. You should check that first and throw a proper error to make debugging easier.

这篇关于ActionScript3的:我怎么能访问外部类的舞台元素呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆