簡體   English   中英

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?

這是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);
...

您可以添加背景 - 透明精靈 - 並使用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);

確保textButton具有更高的優先級:

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

將點擊的精靈(我們的背景)作為第一個參數添加到粒子函數:

function particleBurst(bg, pointer) {

這樣只應觸發一個事件。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM