Wassup,我正在尝试创建一个 HTML 游戏,但无法弄清楚一点代码。我想不断地随机化障碍物的间隔,这样几帧后就没有图案了。
function updateGameArea() {
var x, y, a, b, c, d, e, f, g, h;
for (i = 0; i < myObstacles.length; i += 1) {
if (myGamePiece.crashWith(myObstacles[i])) {
myGameArea.stop();
return;
}
}
myGameArea.clear();
myGameArea.frameNo += 1;
if (myGameArea.frameNo == 1 || everyinterval(600)) {
x = myGameArea.canvas.width;
y = myGameArea.canvas.height - 350;
a = myGameArea.canvas.width - -4444;
b = myGameArea.canvas.height - 750;
c = myGameArea.canvas.width - -20674;
d = myGameArea.canvas.height - 750;
e = myGameArea.canvas.width - -11111;
f = myGameArea.canvas.height - 1150;
g = myGameArea.canvas.width - -40678;
h = myGameArea.canvas.height - 1150;
myObstacles.push(new component(500, 300, "green", x, y));
myObstacles.push(new component(500, 300, "green", a, b));
myObstacles.push(new component(500, 300, "green", c, d));
myObstacles.push(new component(500, 300, "green", e, f));
myObstacles.push(new component(500, 300, "green", g, h));
}
for (i = 0; i < myObstacles.length; i += 1) {
myObstacles[i].x += -5;
myObstacles[i].a += -1;
myObstacles[i].c += -1;
myObstacles[i].e += -1;
myObstacles[i].g += -1;
myObstacles[i].update();
}
myScore.text="SCORE: " + myGameArea.frameNo;
myScore.update();
myGamePiece.newPos();
myGamePiece.update();
}
function everyinterval(n) {
if ((myGameArea.frameNo / n) % 1 == 0) {return true;}
return false;
}
这是我目前所拥有的。请帮助!!!在此处输入代码
最佳答案
您可以使用两个函数,一个用于更新/操作,一个用于使用随机值调用超时。
使用更高级的样式,您可以添加检查下一次超时调用是否应该停止。
function randomTimeout(fn) {
var t = Math.floor(Math.random() * 4 + 2) * 1000;
console.log('randomTimeout', t);
setTimeout(fn, t);
}
function update() {
console.log('update');
randomTimeout(update);
}
update();
关于javascript - 如何随机化游戏的间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46783075/