我需要帮助在 AS3 中冒泡事件
我想让我的班级的父级首先处理该事件,然后我想让孩子处理该事件。有没有办法明确地使事件冒泡?我想做这样的事情:
...
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
事件分为三个“阶段”; 捕获、目标和气泡。它们按此顺序发生,这意味着如果您将事件侦听器设置为处于捕获阶段,它将始终在一组事件之前定期触发(这意味着在目标或气泡处)。
就像这样:
现在,您的父事件侦听器将始终首先触发!
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:
Now, your parent event listener will always fire first!
我想出了一个方法来做到这一点。这看起来很黑客,但它确实有效。如果有更好的方法,请告诉我。
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.
如果您要在父级而不是子级中处理侦听器,可能会更容易。然后你可以在完成后将事件传递给孩子:
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: