我需要帮助在 AS3 中冒泡事件

发布于 2024-12-28 11:58:51 字数 278 浏览 1 评论 0原文

我想让我的班级的父级首先处理该事件,然后我想让孩子处理该事件。有没有办法明确地使事件冒泡?我想做这样的事情:

...
this.addEventListener(MouseEvent.CLICK, characterClicked);
...

private function characterClicked(e:Event):void{
// pass event to parent to be handled first
...
}

这可能吗?如果可能的话怎么办?

I want to have the parent of my class handle the event first, then I want to have the child handle the event. Is there a way to explicitly bubble the event up? I want to do something like this:

...
this.addEventListener(MouseEvent.CLICK, characterClicked);
...

private function characterClicked(e:Event):void{
// pass event to parent to be handled first
...
}

Is this possible, and if so how?

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

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

发布评论

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

评论(3

把梦留给海 2025-01-04 11:58:51

事件分为三个“阶段”; 捕获、目标和气泡。它们按此顺序发生,这意味着如果您将事件侦听器设置为处于捕获阶段,它将始终在一组事件之前定期触发(这意味着在目标或气泡处)。

就像这样:

// in parent, third argument is "use capture"
child.addEventListener(MouseEvent.CLICK, handleClickInParent, true); 

// in child, add listener as usual
addEventListener(MouseEvent.CLICK, handleClick);

现在,您的父事件侦听器将始终首先触发!

There are three "phases" of an event; Capture, At target and Bubble. They occur in this order, meaning that if you set an event listener to be in the capture phase it will always fire before one set regularly (which would mean either at target or bubble).

Like so:

// in parent, third argument is "use capture"
child.addEventListener(MouseEvent.CLICK, handleClickInParent, true); 

// in child, add listener as usual
addEventListener(MouseEvent.CLICK, handleClick);

Now, your parent event listener will always fire first!

风透绣罗衣 2025-01-04 11:58:51

我想出了一个方法来做到这一点。这看起来很黑客,但它确实有效。如果有更好的方法,请告诉我。

...
this.addEventListener(MouseEvent.CLICK, characterClicked);
...

private function characterClicked(e:Event):void{
// pass event to parent to be handled first
this.removeEventListener(MouseEvent.CLICK, characterClicked); //prevent infinite loop
            dispatchEvent(e); // send event to parent object
            this.addEventListener(MouseEvent.CLICK, characterClicked);
            e.stopImmediatePropagation();
...
}

I figured out a way to do this. Is seems hackish, but it works. If there is a better way of doing this please let me know.

...
this.addEventListener(MouseEvent.CLICK, characterClicked);
...

private function characterClicked(e:Event):void{
// pass event to parent to be handled first
this.removeEventListener(MouseEvent.CLICK, characterClicked); //prevent infinite loop
            dispatchEvent(e); // send event to parent object
            this.addEventListener(MouseEvent.CLICK, characterClicked);
            e.stopImmediatePropagation();
...
}
树深时见影 2025-01-04 11:58:51

如果您要在父级而不是子级中处理侦听器,可能会更容易。然后你可以在完成后将事件传递给孩子:

// inside parent class:
childObj.addEventListener(MouseEvent.CLICK, onCharacterClicked);

private function onCharacterClicked(e:Event):void {
    // do parent stuff first
    // ...

    // then pass event to child 
    childObj.onCharacterClicked(e);
}

If you were to handle the listener in the parent instead of the child it might be easier. Then you could just pass the event to the child when you're done:

// inside parent class:
childObj.addEventListener(MouseEvent.CLICK, onCharacterClicked);

private function onCharacterClicked(e:Event):void {
    // do parent stuff first
    // ...

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