Phaser,使按钮点击事件连续
Phaser, make a button click event continuous
我正在使用 Phaser.js 创建一个地图 (tileSprite) 并在上面放置一些精灵,因为不是所有的精灵都能进入,我正在使用相机左右平移。
我希望用户单击键盘键(向左或向右)或方向按钮 sprite 以连续平移相机,直到用户释放控件。
我实现了类似于 this example 的键盘平移,我按住一个键和相机 moves/pans(事件中每边 10 个像素)并在释放键时停止。
但是,当我尝试使用 2 个 sprite 按钮实现相同的功能时,每个按钮仅触发 1 个事件并且每次单击仅将相机平移 10 像素。我需要一直开火直到我松开钥匙。
var panRightButton = game.add.button(800, 5, 'right_pan_btn', onClickAction, this);
panRightButton.onInputOver.add(onButtonOver, this);
panRightButton.onInputOut.add(onButtonOut, this);
panRightButton.onInputDown.add(panScreenRight, this);
function panScreenRight() {
game.camera.x += 10;
}
我试过使用布尔标志 (isPanning
),如果我单击按钮,它会变为真,而在释放时变为假。并在 game.camera.x += 10;
上有一个 while 循环,但它只是减慢并停止了脚本。
function onClickAction() {
isPanning = true;
}
function onButtonOut() {
isPanning = false;
}
function onButtonUp() {
isPanning = false;
}
function panScreenLeft() {
if (isPanning) {
game.camera.x -= 10;
}
}
正确的方法是在 update
方法中,而不是在循环中。使用标志来知道按钮是否被按下是可以的,但只需让 Phaser 更新相机位置,就像您链接的示例中一样:
function update() {
//...
if (isPanningLeft) {
game.camera.x -= 10;
} else if (isPanningRight) {
game.camera.x += 10;
}
//...
}
不需要循环,因为update
方法是在一个循环内执行的(预计按帧执行一次)
我正在使用 Phaser.js 创建一个地图 (tileSprite) 并在上面放置一些精灵,因为不是所有的精灵都能进入,我正在使用相机左右平移。
我希望用户单击键盘键(向左或向右)或方向按钮 sprite 以连续平移相机,直到用户释放控件。
我实现了类似于 this example 的键盘平移,我按住一个键和相机 moves/pans(事件中每边 10 个像素)并在释放键时停止。
但是,当我尝试使用 2 个 sprite 按钮实现相同的功能时,每个按钮仅触发 1 个事件并且每次单击仅将相机平移 10 像素。我需要一直开火直到我松开钥匙。
var panRightButton = game.add.button(800, 5, 'right_pan_btn', onClickAction, this);
panRightButton.onInputOver.add(onButtonOver, this);
panRightButton.onInputOut.add(onButtonOut, this);
panRightButton.onInputDown.add(panScreenRight, this);
function panScreenRight() {
game.camera.x += 10;
}
我试过使用布尔标志 (isPanning
),如果我单击按钮,它会变为真,而在释放时变为假。并在 game.camera.x += 10;
上有一个 while 循环,但它只是减慢并停止了脚本。
function onClickAction() {
isPanning = true;
}
function onButtonOut() {
isPanning = false;
}
function onButtonUp() {
isPanning = false;
}
function panScreenLeft() {
if (isPanning) {
game.camera.x -= 10;
}
}
正确的方法是在 update
方法中,而不是在循环中。使用标志来知道按钮是否被按下是可以的,但只需让 Phaser 更新相机位置,就像您链接的示例中一样:
function update() {
//...
if (isPanningLeft) {
game.camera.x -= 10;
} else if (isPanningRight) {
game.camera.x += 10;
}
//...
}
不需要循环,因为update
方法是在一个循环内执行的(预计按帧执行一次)