Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 什么会导致游戏循环在随机时间崩溃?_Javascript_2d Games_Tetris - Fatal编程技术网

Javascript 什么会导致游戏循环在随机时间崩溃?

Javascript 什么会导致游戏循环在随机时间崩溃?,javascript,2d-games,tetris,Javascript,2d Games,Tetris,我正在做一个项目,在这个项目中,我正在编写一个俄罗斯方块游戏,该游戏将使用Javascript在浏览器中玩。我遇到了一个问题,游戏根本停止工作。它不会给出任何错误消息。这就好像游戏在一段随机的时间(通常是30秒左右)后自动暂停,之后什么都不会发生 我正在使用游戏循环来管理游戏。下面是管理游戏的最高级别代码部分 var cycle = function GameTick(elapsed){ if(menuStatus != "game"){ renderMenu();

我正在做一个项目,在这个项目中,我正在编写一个俄罗斯方块游戏,该游戏将使用Javascript在浏览器中玩。我遇到了一个问题,游戏根本停止工作。它不会给出任何错误消息。这就好像游戏在一段随机的时间(通常是30秒左右)后自动暂停,之后什么都不会发生

我正在使用游戏循环来管理游戏。下面是管理游戏的最高级别代码部分

var cycle = function GameTick(elapsed){

    if(menuStatus != "game"){
        renderMenu();
        updateMenu();
    }

    if(menuStatus == "game"){
        render();
        update();
    }

    requestAnimationFrame(cycle);
}
我的第一反应是,在运行这个函数一段时间后,不断调用GameTick函数的新迭代,它会耗尽堆栈空间,但我不知道这是否正确。谢谢你的帮助,我很乐意澄清任何没有意义的事情。我知道我没有给你太多的东西,但是我不确定除了发布整个游戏代码(非常长)还有什么可以帮助你

编辑:我正在按要求发布我的渲染和更新功能

function update(){

    var currentTime = performance.now();
    currentTime = Math.trunc(currentTime);

    if((currentTime - beginTime) > dropTime)
    {
        pieceMoved = movePieces();
    }

    if(!pieceMoved && (currentTime - beginTime) > dropTime)  //If no pieces can move
    {
        setPieceLocation();
        handleFullRows();
        spawnNewPiece();
        console.log(pieceArray.length)
    }

    if(pieceArray.length > 0)
        console.log(pieceArray[pieceArray.length - 1].color);

    if((currentTime - beginTime) > dropTime)
        beginTime = performance.now();
}

function render(){        //This function handles the rendering
    ctx.fillStyle = "white";
    ctx.fillRect(0, 0, canvas.width, canvas.height);

    if(pieceArray.length > 0)    //This renders the current moving piece
    {
        for(var j = 0; j < 4; j++)
        {
            drawColoredBlock(pieceArray[pieceArray.length - 1].color,
                             pieceArray[pieceArray.length - 1].xCoordinates[j],
                             pieceArray[pieceArray.length - 1].yCoordinates[j]);
        }
    }

    for(var i = 0; i < 10; i++)    //This renders all of the pieces that have been placed
    {

        for(var j = 0; j < 20; j++)
        {

            if(gameBoard[i][j] != 'white')
            {
                drawColoredBlock(gameBoard[i][j], i, j);
            }
        }
    }
}
函数更新(){
var currentTime=performance.now();
currentTime=Math.trunc(currentTime);
如果((currentTime-beginTime)>dropTime)
{
pieceMoved=movePieces();
}
if(!pieceMoved&(currentTime-beginTime)>dropTime)//如果没有片段可以移动
{
setPieceLocation();
handleFullRows();
新件();
console.log(pieceArray.length)
}
如果(pieceArray.length>0)
log(pieceArray[pieceArray.length-1].color);
如果((currentTime-beginTime)>dropTime)
beginTime=performance.now();
}
函数render(){//此函数处理渲染
ctx.fillStyle=“白色”;
ctx.fillRect(0,0,canvas.width,canvas.height);
if(pieceArray.length>0)//这将渲染当前移动的工件
{
对于(var j=0;j<4;j++)
{
drawColoredBlock(pieceArray[pieceArray.length-1]。颜色,
pieceArray[pieceArray.length-1].xCoordinates[j],
pieceArray[pieceArray.length-1].yCoordinates[j]);
}
}
for(var i=0;i<10;i++)//这将渲染已放置的所有片段
{
对于(var j=0;j<20;j++)
{
如果(游戏板[i][j]!=“白色”)
{
drawColoredBlock(游戏板[i][j],i,j);
}
}
}
}

render*/update*包含什么?如果循环的第1行是一个
console.log
,它在“崩溃”后是否继续记录?是否尝试点击F12检查是否有任何错误?是的,我已经查看了控制台。没有错误。console.log函数在停止工作后继续运行。快速查看它,我认为您的计时有问题。你能为你所拥有的东西做一个极简主义的演示,这样我们可以更仔细地检查一下吗?(小提琴或内联)。我特别感兴趣的是你如何设置时间;而且你从不使用经过的时间。也不需要缩短时间。。