Phaser JS如何阻止事件从textButton.events.onInputDown事件传播(触发)到game.input.onDown事件? [英] Phaser JS how to stop event Propagation(firing) from textButton.events.onInputDown event to game.input.onDown event?

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

问题描述

以下是 JSFiddle

Here is the JSFiddle.

我在这里有两个事件。


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

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

  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.


使用priorityID以确定在输入事件发生时哪些游戏对象应该获得
优先级。例如,如果您有几个重叠的
Sprites,则默认情况下,显示
列表顶部的一个为输入事件提供优先级。您可以通过控制priorityID值来阻止
发生。价值越高,
对于输入事件就越重要。

The priorityID is used to determine which game objects should get priority when input events occur. For example if you have several Sprites that overlap, by default the one at the top of the display list is given priority for input events. You can stop this from happening by controlling the priorityID value. The higher the value, the more important they are considered to the Input events.

请参阅: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);

确保您的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.

查看修改的提示: http://jsfiddle.net/g05bbL6g/3/

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆