gpt4 book ai didi

javascript - javascript 函数的第二部分未运行?

转载 作者:行者123 更新时间:2023-11-28 17:54:42 25 4
gpt4 key购买 nike

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

功能 block 逻辑未运行注释“//COLLISION!”下面的代码它可能非常简单,但我刚刚开始使用 javascript 进行编码(这就是为什么我的代码格式很糟糕)我添加了很多调试 console.logs 来查看正在运行的内容和未运行的内容。

最佳答案

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

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

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

关于javascript - javascript 函数的第二部分未运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44740811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com