获取时返回键盘输入的函数

时间:2019-04-24 18:22:08

标签: javascript canvas input

我正在用HTML画布用JavaScript编写游戏。我需要一个函数,等待用户在指定范围内从键盘提供输入,然后返回到被调用的地方。

我有用于键释放的事件侦听器,仅当'waitingForInput'为true时,才将值传递给变量'collectedInput'。可悲的是,这堆代码不起作用。它只是不返回呼叫位置。

var getInput = function (startingIndex, endingIndex){
    waitingForInput = true;
    startingIndex = startingIndex? startingIndex: 1;//default range
    endingIndex = endingIndex? endingIndex: 6;
    if (collectedInput==0&&waitingForInput){
        setTimeout(getInput(startingIndex, endingIndex), 1000);
    }
    else if (collectedInput<startingIndex||collectedInput>endingIndex){
        setTimeout(getInput(startingIndex, endingIndex), 1000);
    }
    else{
        waitingForInput = false;
        return collectedInput;
    }
}

我也尝试过:

var getInput = function(){
    while (collectedInput==0){
        pause(10);
    }
    return;
}
function pause(numberMillis) { 
    var now = new Date(); 
    var exitTime = now.getTime() + numberMillis; 
    while (true) { 
        now = new Date(); 
        if (now.getTime() > exitTime) 
            return; 
    } 
} 

但它只会使浏览器崩溃。

0 个答案:

没有答案
相关问题