Flash 自动将舞台上的对象命名为“实例#”

发布于 2024-10-09 15:01:23 字数 1182 浏览 3 评论 0原文

我的主舞台上已经放置了 2 个 TLF 文本框。在属性检查器窗口中,我为它们指定了实例名称:“txt1”和“txt2”。

我试图有一个 mouseup 事件,并找出它发生在哪个文本框上。

我的文档类具有以下代码:

package  {
import flash.display.Sprite;
import flash.events.KeyboardEvent;

public class SingleEvent extends Sprite{

    public function SingleEvent() {
        // constructor code
        root.addEventListener(KeyboardEvent.KEY_UP, textChanged,false,0,true);
    }

    private function textChanged(e:KeyboardEvent){
        trace(e.target.name);
        trace("   " + e.target);
        switch(e.target){
            case txt1:
                trace("txt1 is active");
                break;
            case txt2:
                trace("txt2 is active");
                break;
            default:
                break;
        }
    }

    }
}

示例输出为:

instance15
   [object Sprite]
instance21
   [object Sprite]

由于对象已经在舞台上,我不确定如何让 flash 将它们识别为“txt1”和“txt2”而不是“instance#”。我尝试设置 .name 属性,但没有效果。

在发布设置中,我选中了“自动声明阶段实例”。

另外,是否可以为多个滑块组件提供一个更改事件?以下永远不会触发:

root.addEventListener(SliderEvent.CHANGE, sliderChanged,false,0,true);

感谢您的任何提示

I have 2 TLF text boxes already placed on my main stage. In the property inspector window I give these the instance names: "txt1" and "txt2".

I am trying to have a single mouseup event, and figure out which text box it occurred on.

My document class has the following code:

package  {
import flash.display.Sprite;
import flash.events.KeyboardEvent;

public class SingleEvent extends Sprite{

    public function SingleEvent() {
        // constructor code
        root.addEventListener(KeyboardEvent.KEY_UP, textChanged,false,0,true);
    }

    private function textChanged(e:KeyboardEvent){
        trace(e.target.name);
        trace("   " + e.target);
        switch(e.target){
            case txt1:
                trace("txt1 is active");
                break;
            case txt2:
                trace("txt2 is active");
                break;
            default:
                break;
        }
    }

    }
}

Example output is:

instance15
   [object Sprite]
instance21
   [object Sprite]

Since the objects are already on the stage, I am not sure how to get flash to recognize them as "txt1" and "txt2" instead of "instance#". I tried setting the .name property, but it had no effect.

In the publish settings, I have "Automatically declare stage instances" checked.

Also, is it possible to have a single change event for multiple slider components? The following never fires:

root.addEventListener(SliderEvent.CHANGE, sliderChanged,false,0,true);

Thanks for any tips

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生一梦 2024-10-16 15:01:23

在调用构造函数时,Flash Player 似乎尚未初始化 TLFTextFields。试试这个:

package  {

    import flash.display.MovieClip;
    import flash.utils.describeType;
    import flash.events.Event;

    public class TestTLF extends MovieClip {

        private function onEnterFrame (ev:Event) : void {
            removeEventListener (Event.ENTER_FRAME, onEnterFrame);
            for (var i:int = 0; i < numChildren; i++) {
                var obj:Object = getChildAt(i);
                trace (obj.name + ":"+describeType(obj));
            }
        }

        public function TestTLF() {
            addEventListener (Event.ENTER_FRAME, onEnterFrame);
        }
    }

}

您会发现除了神秘创建的附加对象“__id#”和“instance#”之外,您的两个 TFLTextField 都被识别。

另外,您的 Slider 组件显然不会“冒泡”其 SliderEvents,这意味着它们永远不会到达根对象。但是,您可以根据需要多次分配处理程序函数,因此您只需向每个滑块添加侦听器,所有侦听器都引用相同的 sliderChanged 函数。

It seems that the Flash Player hasn't initialized the TLFTextFields by the time the constructor is called. Try this:

package  {

    import flash.display.MovieClip;
    import flash.utils.describeType;
    import flash.events.Event;

    public class TestTLF extends MovieClip {

        private function onEnterFrame (ev:Event) : void {
            removeEventListener (Event.ENTER_FRAME, onEnterFrame);
            for (var i:int = 0; i < numChildren; i++) {
                var obj:Object = getChildAt(i);
                trace (obj.name + ":"+describeType(obj));
            }
        }

        public function TestTLF() {
            addEventListener (Event.ENTER_FRAME, onEnterFrame);
        }
    }

}

You will find that in addition to the mysteriously created additional objects "__id#" and "instance#", both of your TFLTextFields are recognized.

Also, your Slider components obviously don't "bubble" their SliderEvents, which means they never reach the root object. You can, however assign a handler function as many times as you want, so you could just add listeners to each of your sliders, all referring to the same sliderChanged function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文