简体   繁体   English

闲置5秒后结束游戏

[英]End the game after 5 seconds of inactivity

I'm new to Phaser.io so I do apologize if this post isn't well. 我是Phaser.io的新手,所以如果这篇文章不好,我会道歉。

How to end the game after 5 seconds of inactivity ? 闲置5秒后如何结束游戏? I've made something but I think it is really really bad for performance. 我做了一些事情,但我认为它确实对性能不利。 Each time the update function is called I check if the user didn't pushed "up, left, right, down" and after I check if we have exceed the time by doing (currentTime - beginningTime > 5000). 每次调用更新函数时,我都会检查用户是否没有按“上,左,右,下”按钮,并且在执行后检查是否超过了时间(currentTime-BeginningTime> 5000)。 This code has 2 problems I'm trying to fire : - The performance is really bad because it is not necessary too check every time we call the update function - In my condition I want to check that "no key has been pushed" and for now I just check if the user didn't pushed "up", "left", "right" or "down" 此代码有2个我要触发的问题:-性能真的很差,因为每次调用更新函数时都不必检查-在我的情况下,我想检查“没有按下任何键”,并且现在我只是检查用户是否没有按“上”,“左”,“右”或“下”

How to do that ? 怎么做 ? Sorry for my english 对不起我的英语不好

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();
    }
}

You can add global keyboard event listener by game.input.keyboard.addCallbacks 您可以通过game.input.keyboard.addCallbacks添加全局键盘事件监听

var timeBeginning = Date.now();

function create(){
    function updateTime(){
        timeBeginning = Date.now();
    }
    game.input.keyboard.addCallbacks(game, updateTime, updateTime);
}

function update() {
    //your code

    if (Date.now() - timeBeginning > 5000) {
        end();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM