我遇到了一个问题,需要以与 Phaser.Key.reset 相同的方式重置 game.input.onDown
事件。 。似乎这可以通过 Phaser.Pointer.reset 来实现。方法。不幸的是,Phaser.Input 类中似乎有一堆不同的潜在指针对象,并且我不确定需要将哪一个用于 game.input.onDown
,即 says that it gets "dispatched each time a pointer is pressed down" 。我只是不太确定我需要使用哪个指针。有人能解释一下吗?
我想我实际上需要做这样的事情:
this.game.input.whicheverPointer.reset();
编辑:
Here is an example that mimics the the problem I'm having.
这是它的源代码:
var game = new Phaser.Game(800, 600, Phaser.CANVAS, "game", {preload: preload, create: create, update: update});
var dude;
var block;
var spacebar;
var gameOverText;
var gameOverCounter = 0;
var gameOverBool = false;
function preload () {
game.load.image("block", "assets/block.png");
game.load.image("dude", "assets/phaser-dude.png");
}
function create () {
dude = game.add.sprite(373, 760, "dude");
block = game.add.sprite(0, 505, "block");
game.physics.arcade.enable(dude);
game.physics.arcade.enable(block);
dude.body.collideWorldBounds = true;
dude.body.gravity.y = 200;
block.body.collideWorldBounds = true;
block.body.immovable = true;
block.body.velocity.x = 100;
block.body.bounce.x = 1;
spacebar = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
var jumpOrTryAgain = function () {
if (gameOverBool === false) {
dude.body.velocity.y = -250;
// If you open up your JS / error console, you'll see that
// this message gets printed an extra time each reset,
// when you click. The spacebar doesn't have this problem
// because of the `spacebar.reset()` call below.
console.log("down");
} else {
dude.destroy();
block.destroy();
gameOverText.destroy();
// Here, I can reset the spacebar, but I'm not sure what to do
// for the click / touch event, which keeps getting the jumpOrTryAgain
// callback added onto it.
spacebar.reset();
gameOverBool = false;
gameOverCounter = 0;
create();
}
};
game.input.onDown.add(jumpOrTryAgain);
spacebar.onDown.add(jumpOrTryAgain);
}
function update () {
function gameOver () {
if (gameOverCounter === 0) {
gameOverBool = true;
dude.body.velocity.y = 0;
dude.body.velocity.x = 0;
block.body.velocity.x = 0;
gameOverText = game.add.text(300, 200, "Game Over!", {fontSize: "16px", fill: "white"});
gameOverCounter += 1;
}
}
game.physics.arcade.collide(dude, block, gameOver);
}
正如您在上面的评论中所读到的,问题在于,当您递归调用 create
时,单击/触摸事件不断将 jumpOrTryAgain
回调添加到自身上。重置游戏。我需要一种重置点击/触摸事件的方法,类似于上面所示的 spacebar.reset()
。
最佳答案
game.input.onDown
由鼠标和触摸输入触发。如果您需要在每次调用后重置其回调,则可以使用 game.input.onDown.addOnce()
(而不是 game.input.onDown.add()
)以便回调在第一次运行后自动删除。
关于javascript - 重置 game.input.onDown 时使用哪个指针对象(即光标),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30736948/