本文介绍了Phaser JS如何阻止事件从textButton.events.onInputDown事件传播(触发)到game.input.onDown事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是

Here is the JSFiddle.

我在这里有两个事件。


  1. 游戏。输入.onDown 这是一些逻辑(在我的例子中生成粒子)

  2. textButton.events。 onInputDown ,其中textButton是 对象实例,这是另一个逻辑。

  1. Is game.input.onDown which does some logic (generates particles in my example)
  2. Is textButton.events.onInputDown, where textButton is a Phaser.Text object instance, which does another logic.

问题是:当我点击我的 textButton 两个事件都被触发 1 2

The problem is: when I click on my textButton both event are fired 1 and 2.

问题是如何防止点击 textButton 时,点击 1

The question is, how to prevent event 1 from firing when I click on the textButton?

部分代码:

...
//This event is fired on click anywhere event # 1
game.input.onDown.add(particleBurst, this);

//This is Clickable text
textButton = game.add.text(game.world.width - 5, 5, "CLICK ME", fontStyle);
textButton.anchor.setTo(1, 0);
textButton.inputEnabled = true;

//This event is fired on click on text event # 2
textButton.events.onInputDown.add(function () {
    console.log("button is Clicked");
}, this, 2);
...


推荐答案

您可以添加背景 - 透明的精灵 - 并使用 input.priorityID

You can add a background - transparent sprite - and use input.priorityID.

请参阅:

// This is event #1 added to background sprite
var bg = game.add.sprite(0, 0);
bg.fixedToCamera = true;
bg.scale.setTo(game.width, game.height);
bg.inputEnabled = true;
bg.input.priorityID = 0; // lower priority
bg.events.onInputDown.add(particleBurst);

确保您的textButton具有更高的优先级:

Make sure your textButton has higher priority:

textButton.input.priorityID = 1; // higher pirority

将点击的sprite(我们的背景)添加为粒子函数的第一个参数:

Add the clicked sprite (our background) as a first parameter to the particle function:

function particleBurst(bg, pointer) {

这样只能触发一个事件。

This way only one event should be triggered.

查看修改的提示:

这篇关于Phaser JS如何阻止事件从textButton.events.onInputDown事件传播(触发)到game.input.onDown事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 05:57