【问题标题】:Phaser JS how to stop event Propagation(firing) from textButton.events.onInputDown event to game.input.onDown event?Phaser JS如何停止从textButton.events.onInputDown事件到game.input.onDown事件的事件传播(触发)?
【发布时间】:2015-02-23 09:38:44
【问题描述】:

这里是JSFiddle

我在这里有两个活动。

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

问题是:当我点击 textButton 时,两个事件都会触发 12

问题是,当我点击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);
...

【问题讨论】:

    标签: javascript events html5-canvas phaser-framework


    【解决方案1】:

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

    priorityID 用于确定应该获取哪些游戏对象 输入事件发生时的优先级。例如,如果您有几个 重叠的精灵,默认情况下显示顶部的精灵 list 被赋予输入事件的优先级。你可以阻止这个 通过控制priorityID 值发生。价值越高, 它们对输入事件越重要。

    见: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 具有更高的优先级:

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

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

    function particleBurst(bg, pointer) {
    

    这样应该只触发一个事件。

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

    【讨论】:

    • 谢谢@lpiepiora。这绝对有效,恕我直言,这是一个很好的解决方法。但是我想知道是否有任何全局游戏输入的解决方案?正如我尝试过的那样,我自己的优先级在这种情况下不起作用。
    • @AlexanderArutinyants 我不知道更好的方法,如果有的话我很乐意自己去了解它;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多