向嵌套组件添加功能

发布于 2024-11-04 11:21:06 字数 1603 浏览 1 评论 0原文

好的,情况是这样...

我有一个自定义 mxml 组件,其中包含几个图像和 4 个按钮。组件文件已包含每个按钮的 clickHandler。我需要能够访问 clickHandler 或创建另一个函数并将其附加到我的 Main.mxml 文件中的这些按钮。我应该添加到原始的 clickHandlers 中吗?如果是这样,如何将该方法应用于我的 Main.mxml 文件?

仅供参考:该组件有 5 个状态,每个 clickHandler 都会触发状态之间的转换。

这是组件文件中的两个 clickHandlers

protected function submit_clickHandler():void
        {
            const state:String = currentState;
            if ( state == 'state1' ) {
                currentState='state2';
            }
            if ( state == 'state3' ) {
                currentState='state4';
                addElement(images[i]);     //The methods I want to run from 
                getStudentAnswer();        //within the Main.mxml.
                submit();                  //If I add them here, I get an
                newQuestion();             //undefined method error.
            }
            if ( state == 'state4' ) {
                currentState='state4';
            }
            if ( state == 'state5' ) {
                currentState='state4';
            }
            if ( state == 'state3' ) {
                Sequence1.play();
            }
        }

        protected function checkAnswer_clickHandler():void
        {
            const state:String = currentState;
            if ( state == 'state2' ) {
                currentState='state1';
            }
            if ( state == 'state4' ) {
                currentState='state5';
            }
        }

谢谢, 杰明

O.K. Here's the situation...

I have a custom mxml component that contains a couple of images and 4 buttons. The component file already contains a clickHandler for each of the buttons. I need to be able to either access the clickHandler or create another function and attach it to those buttons from within my Main.mxml file. Should I add to the original clickHandlers? if so, how do I apply the method to my Main.mxml file?

FYI: The component has 5 states and each clickHandler triggers transitions between the states.

Here are the two clickHandlers from the component file:

protected function submit_clickHandler():void
        {
            const state:String = currentState;
            if ( state == 'state1' ) {
                currentState='state2';
            }
            if ( state == 'state3' ) {
                currentState='state4';
                addElement(images[i]);     //The methods I want to run from 
                getStudentAnswer();        //within the Main.mxml.
                submit();                  //If I add them here, I get an
                newQuestion();             //undefined method error.
            }
            if ( state == 'state4' ) {
                currentState='state4';
            }
            if ( state == 'state5' ) {
                currentState='state4';
            }
            if ( state == 'state3' ) {
                Sequence1.play();
            }
        }

        protected function checkAnswer_clickHandler():void
        {
            const state:String = currentState;
            if ( state == 'state2' ) {
                currentState='state1';
            }
            if ( state == 'state4' ) {
                currentState='state5';
            }
        }

Thanks,
JM

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

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

发布评论

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

评论(1

笔落惊风雨 2024-11-11 11:21:06

如果您有权访问按钮的调度事件,只需将“bubbles”设置为“true”并侦听 Main.mxml 中的事件。如果您将显示列表视为具有分支的层次树,则冒泡的事件将沿分支向上移动,直到到达第一个 DisplayObject。在此过程中,它会检查每个 DisplayObject 以查找它是否正在等待捕获事件。

下面是一个基本示例,其中 Main 正在侦听单击事件,并且有一个子 MovieClip 实例和一个子 Sprite 实例。 Sprite 实例有一个正在调度单击事件的子 Button 实例。

[object Main] (listening for MouseEvent.CLICK)
      |
      |__[object MovieClip]
                |
                |___[object Sprite]
                          |
                          |____[object Button] (dispatching MouseEvent.CLICK)

Button 中分派的单击事件将是:

dispatchEvent(new MouseEvent(MouseEvent.CLICK, true));

在 Main 中,您只需监听该事件:

addEventListener(MouseEvent.CLICK, clickHandler);

因为 MouseEvent.CLICK 是一个常见事件,所以我建议在这种类型中创建一个自定义事件(或至少一个唯一的事件名称)场景以避免 DisplayObject 捕获错误事件的可能性。但是,希望这能让您更好地了解如何处理冒泡。

-

如果没有冒泡,捕获事件的唯一其他方法是使用点语法。我不推荐这种方法,因为它可能会变得混乱,但这有助于说明冒泡的概念(假设每个子 DisplayObject 都是公共的):

In Main:(再次,不推荐)

movieClip.sprite.button.addEventListener(MouseEvent.CLICK, clickHandler);

希望有所帮助!

[编辑 - 添加自定义事件]
自定义事件类如下所示:

package com.example.events
{
    import flash.events.Event;

    public class CustomEvent extends Event {

        public static const WITH_DATA:String = "withData";

        public var data:Object;

        public function NavEvent(type:String, data:Object=null, bubbles:Boolean=false, cancelable:Boolean=false) {
            super(type, bubbles, cancelable);
            this.data = data;
        }
    }
}

然后您将调度一个事件,如下所示:

var myString:String = "buttonState";
dispatchEvent(new CustomEvent(CustomEvent.WITH_DATA, myString, true));

并侦听该事件,如下所示:

addEventListener(CustomEvent.WITH_DATA, customEventHandler);

private function customEventHandler(e:CustomEvent):void {
    trace(e.data);
}

If you have access to the dispatch event for the buttons, just set "bubbles" to "true" and listen for the event in Main.mxml. If you think of the display list as a hierarchal tree with branches, an event that bubbles will travel up the branch until it reaches the first DisplayObject. Along the way, it checks each DisplayObject to find if it is waiting to capture the event.

Here is a basic illustration where Main is listening for a click event and has a child MovieClip instance with a child Sprite instance. The Sprite instance has a child Button instance that is dispatching a click event.

[object Main] (listening for MouseEvent.CLICK)
      |
      |__[object MovieClip]
                |
                |___[object Sprite]
                          |
                          |____[object Button] (dispatching MouseEvent.CLICK)

The dispatched click event in Button would be:

dispatchEvent(new MouseEvent(MouseEvent.CLICK, true));

In Main, you would just listend for the event:

addEventListener(MouseEvent.CLICK, clickHandler);

Because MouseEvent.CLICK is such a common event, I would suggest creating a custom event (or at least a unique event name) in this type of scenario to avoid the possibility of DisplayObject's capturing incorrect events. But, hopefully this gives a better idea of how to handle bubbling.

-

Without bubbling, the only other way to capture the event would be to use dot syntax. I don't recommend this method because it could get messy, but this helps to illustrate the concept of bubbling (assuming each child DisplayObject is public):

In Main: (again, not recommended)

movieClip.sprite.button.addEventListener(MouseEvent.CLICK, clickHandler);

Hope that helps!

[EDIT - Added custom Event]
Here is what a custom Event class would look like:

package com.example.events
{
    import flash.events.Event;

    public class CustomEvent extends Event {

        public static const WITH_DATA:String = "withData";

        public var data:Object;

        public function NavEvent(type:String, data:Object=null, bubbles:Boolean=false, cancelable:Boolean=false) {
            super(type, bubbles, cancelable);
            this.data = data;
        }
    }
}

You would then dispatch an event like:

var myString:String = "buttonState";
dispatchEvent(new CustomEvent(CustomEvent.WITH_DATA, myString, true));

And listen for the event like:

addEventListener(CustomEvent.WITH_DATA, customEventHandler);

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