Javascript 5秒不活动后结束游戏
我是Phaser.io的新手,所以如果这篇文章不好,我很抱歉 5秒不活动后如何结束游戏?我做了一些东西,但我认为这对表现真的很糟糕。 每次调用更新函数时,我都会检查用户是否没有按“向上、向左、向右、向下”键,然后检查我们是否已通过执行操作超过了时间(currentTime-BeginingTime>5000)。 此代码有两个问题,我正试图触发: -性能非常差,因为没有必要每次调用update函数时都进行检查 -在我的情况下,我想检查“没有按下任何键”,现在我只检查用户是否没有按下“向上”、“向左”、“向右”或“向下” 怎么做? 对不起,我的英语不好Javascript 5秒不活动后结束游戏,javascript,phaser-framework,Javascript,Phaser Framework,我是Phaser.io的新手,所以如果这篇文章不好,我很抱歉 5秒不活动后如何结束游戏?我做了一些东西,但我认为这对表现真的很糟糕。 每次调用更新函数时,我都会检查用户是否没有按“向上、向左、向右、向下”键,然后检查我们是否已通过执行操作超过了时间(currentTime-BeginingTime>5000)。 此代码有两个问题,我正试图触发: -性能非常差,因为没有必要每次调用update函数时都进行检查 -在我的情况下,我想检查“没有按下任何键”,现在我只检查用户是否没有按下“向上”、“向左
var timeBeginning = new Date().getTime();
function update() {
// input to move the ship
if (cursors.up.isDown) {
game.physics.arcade.accelerationFromRotation(ship.rotation, 200, ship.body.acceleration);
} else {
// stopper the acceleration
ship.body.acceleration.set(0);
}
if (cursors.left.isDown) {
ship.body.angularVelocity = -300;
} else if (cursors.right.isDown) {
ship.body.angularVelocity = 300;
} else {
// stop the rotation
ship.body.angularVelocity = 0;
}
if (!cursors.up.isDown && !cursors.left.isDown && !cursors.right.isDown && !cursors.down.isDown) {
if (new Date().getTime() - timeBeginning > 5000) {
end();
}
} else {
timeBeginning = new Date().getTime();
}
}
您可以通过game.input.keyboard.addCallbacks添加全局键盘事件侦听器
var timebeging=Date.now();
函数create(){
函数updateTime(){
timestart=Date.now();
}
game.input.keyboard.addCallbacks(game,updateTime,updateTime);
}
函数更新(){
//你的代码
如果(Date.now()-timebeging>5000){
end();
}
}