function blocksLogic(){

if(gameRunning == 0){
    var blocks = new Array(7);
    for(var i=0; i <7; i++){
        blocks[i] = new Array(7);
    }
    for(var x=0; x < 7; x++){
        for(var y=0; y < 7; y++){
            blocks[x][y] = false;
            console.log(blocks[x][y]);
        }
    }
}
console.log("gamerunning function ran");
// COLLISION!!!!!!!
for(var brickX = 0; x < 7; x++){
    console.log("for x has been run!");
    for( var brickY = 0; y < 7; y++){
            console.log("for y has been run!");
            var tempBrickX = brickX * 105 + 34;
            var tempBrickY = brickY * 25 - 10;
            //top collision
        if(ballY >= tempBrickX && ballX >= tempBrickX && ballX <= tempBrickX + BRICK_WIDTH){
            console.log("The top of this block has been hit!");
            ballSpeedX = -ballSpeedX;
            ballSpeedY = -ballSpeedY;
        }
            //bottom collision
        if(ballY <= tempBrickY + BRICK_HEIGHT && ballX >= tempBrickX  && ballX >= tempBrickX){
            console.log("The bottom of this brick has been hit!");
            ballSpeedX = -ballSpeedX;
            ballSpeedY = -ballSpeedY;
        }
    }
}


https://pastebin.com/t2Zq79BG

函数blockLogic没有运行注释“ // COLLISION!”下面的代码。
它可能确实很简单,但是我刚开始使用javascript进行编码(这就是为什么我的代码格式很烂)
我添加了许多调试console.logs,以查看正在运行的内容和浪费的时间。

最佳答案

for(var brickX = 0; x < 7; x++)

x在这里未定义,这就是循环永远不会运行的原因(这看起来像是上面的复制粘贴错误)。确保改为使用brickX:

for(var brickX = 0; brickX < 7; brickX++)

08-25 18:57