Phaser JS如何从textButton.events.onInputDown事件到game.input.onDown事件停止事件传播(触发)?

Ale*_*nts 6 javascript events html5-canvas phaser-framework

这是JSFiddle.

我这里有两件事.

  1. 是game.input.onDown,做一些逻辑(在我的例子生成的颗粒)
  2. 是textButton.events.onInputDown,textButton是一个Phaser.Text对象实例,它执行另一个逻辑.

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

问题是,当我点击textButton时如何防止事件1被触发?

部分代码:

...
//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);
...
Run Code Online (Sandbox Code Playgroud)

lpi*_*ora 9

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

priorityID用于确定在输入事件发生时哪些游戏对象应获得优先级.例如,如果您有多个重叠的Sprite,默认情况下,显示列表顶部的Sprite为输入事件赋予优先级.您可以通过控制priorityID值来阻止这种情况发生.值越高,它们对Input事件的考虑就越重要.

请参阅:http://docs.phaser.io/InputHandler.js.html#sunlight-1-line-45

// 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);
Run Code Online (Sandbox Code Playgroud)

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

textButton.input.priorityID = 1; // higher pirority
Run Code Online (Sandbox Code Playgroud)

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

function particleBurst(bg, pointer) {
Run Code Online (Sandbox Code Playgroud)

这样只应触发一个事件.

看看修改过的小提琴:http://jsfiddle.net/g05bbL6g/3/