我正在制作一个蛇游戏,正如您从我之前的问题中可能知道的那样。我正在跟进做一个这样的数组:
var snakeX = new Array();
var snakeY = new Array();
然后在for循环中:
for (var i = 0;i<=num;i++) {
snakeX[0] = xCoord;
snakeY[0] = yCoord;
context.fillRect(snakeX[0], snakeY[0], sH, sW);
xCoord = xCoord + 11;
console.log('xCoord = ' + xCoord + ', yCoord = ' + yCoord);
}
和varsxCoord
都yCoord
设置为0
.
我的问题是为什么这不起作用?
setInterval(function() {
num++;
}, 1000);
这是我所拥有的一切:
function snake() {
//Let's make the snake start at 4 squares wide, and 1 square tall
function createSnake() {
var xCoord = 0;
var yCoord = 0;
var snakeX = new Array();
var snakeY = new Array();
var num = 3;
setInterval(function() {
num = num + 1;
}, 1000);
for (var i = 0;i<=num;i++) {
snakeX[0] = xCoord;
snakeY[0] = yCoord;
context.fillRect(snakeX[0], snakeY[0], sH, sW);
xCoord = xCoord + 11;
console.log('xCoord = ' + xCoord + ', yCoord = ' + yCoord);
}
}
createSnake();
}
snake();