这个问题是针对我在 Code.org 的 Game Lab 中开发的一款游戏。有问题的游戏就像跳伞游戏,障碍物以随机间隔(1000 到 3000 毫秒之间)产生。这是代码:
World.frameRate = 10;
var rects = createGroup();
var timeout = Math.round(randomNumber(800, 3000) / 100) * 100;
function draw() {
background("white");
drawSprites();
if (World.frameCount === Math.round(randomNumber(1000, 3000) / 100) * 100) {
World.frameCount = 0;
creation();
}
}
function creation() {
var obstacle = createSprite(randomNumber(100, 300), 200, 50, 20);
obstacle.velocityY = -2;
}
/*
setInterval(function() {
creation();
}, Math.round(randomNumber(800,3000)/100)*100);
*/
为了创建生成机制,我尝试使用setInterval()
andsetTimeout()
函数,但是对于这两个函数,在第一次执行时,它会创建一个随机数,然后从那里继续以相同的间隔生成对象。我找不到任何解决方法,所以我转向World.frameCount
. 这个概念有效,但是,由于===
操作员的原因,障碍物只会在 1 到 3 秒之间生成,3 秒后,物体将无法生成。为了解决这个问题,我在第 9 行 ( World.frameCount = 0;
) 中添加了。运行时,这会产生错误:
ERROR: Line: 9: TypeError: Cannot set property frameCount of #<Object> which has only a getter
我了解此错误是因为您无法更改World.frameCount
. 有什么解决方法吗?我也将不胜感激有关其他方式的任何建议。
这是 Code.org 项目的链接:Skydive v1.0