javascript - 是什么导致我的 Javascript 游戏在其他计算机上表现不同?
问题描述
所以我在我的电脑上创建了一个简单的游戏,它随机产生圆圈,你必须躲避它们,这样我才能学习一点 JavaScript。在我的电脑上,敌人会随着时间的推移不断产生,但在我的笔记本电脑上,它就像等待 20 个敌人产生,然后它们都开始一起移动。我不确定为什么会发生这种情况以及如何避免这样的事情。我在两台机器上使用相同版本的 Google Chrome。我相信有问题的代码与我的生成敌人功能有关。我在 0-3 之间选择一个随机数,以便我可以选择在哪个边缘生成敌人,然后我利用时间以不同的间隔随机生成敌人。
我的其余代码也可以在这里找到:http: //jsfiddle.net/54dos0e9/
另外,如果有人对如何在不必使用不同机器的情况下测试这些类型的问题有任何建议,我想知道什么是最好的。谢谢你的时间。
function renderEnemies(time) {
var randomStartX = 0;
var randomStartY = 0;
var rand = Math.floor(Math.random() * 4);
switch (rand) {
case 0:
randomStartX = Math.floor(Math.random() * c.width);
randomStartY = 0;
break;
case 1:
randomStartX = 0;
randomStartY = Math.floor(Math.random() * c.height);
break;
case 2:
randomStartX = c.width;
randomStartY = Math.floor(Math.random() * c.height);
break;
case 3:
randomStartX = Math.floor(Math.random() * c.width);
randomStartY = c.height;
break;
}
////////////////////////////////////////////////////////////////////////////////////
if (Math.round(time) % difficulty.amount == 0) {
//console.log("WIDTH HEIGHT " + c.height);
//console.log("RAND IS "+ rand);
//console.log("X is " + randomStartX);
//console.log("Y is " + randomStartY);
//console.log("CANVAS" + c.width);
enemies.push({
x: randomStartX,
y: randomStartY,
edge: rand,
tempX: randomStartX,
tempY: randomStartY
});
//console.log(enemies.length);
}
//To prevent the enemy list from growing too large, we remove from the beginning to free up memory
if (enemies.length == 50) {
enemies.splice(0, 25);
}
enemies.forEach(function(p) {
ctx.save();
ctx.beginPath();
ctx.translate(p.x, p.y);
ctx.arc(0, 0, 10, 0, 2 * Math.PI);
ctx.fillStyle = "red";
ctx.fill();
ctx.stroke();
ctx.restore();
hitbox(p);
});
//console.log(enemies.length);
/////////////////////////////////
}
编辑:在评论中说了这一点,但我意识到这与处理我的时间变量的 if 语句有关。由于某种原因,在我的笔记本电脑上,它在几秒钟内从未找到一个可整除的数字,然后突然之间它一次找到了 10 个。不过,我不确定为什么会发生这种情况,必须弄乱这些值并找出更好的时间间隔。
解决方案
推荐阅读
- python - 您如何为同一点绘制多种颜色?
- flutter - Flutter,无密码并使用电子邮件和密码注册,端点是什么?
- python - 无法通过 pip install 安装 Python 包 hdt。它收到一个错误:命令失败,退出状态为 2
- javascript - 仅从字符串中获取时间
- python - 时间戳 - 如何使用 groupby 计算以秒为单位的时间差
- r - R中的Mclust和sklearn中的高斯混合之间的区别
- python - 自定义 BootstrapDateTimePickerInput
- r - 包含带有小号或等号的表达式的标签的粗体
- python - 在圆圈周围放置文字
- postgresql - 由于身份验证失败,无法访问 psql