所以我正在使用HTML5画布和JavaScript一个简单的物理模拟。我试图做出一些实际的碰撞,但是每当碰撞发生时代码开始无限循环并冻结页面。的Javascript,无限循环和冻结
我使用谷歌浏览器测试版24.0.1312.32-M
当在JavaScript控制台看行“的console.log(”我的东西碰撞“)”都疯了,并印制数千次每第二,彻底打破了页面。
我真的不知道为什么它发生,我不知道该怎么做。任何帮助和/或输入将真正赞赏。
for (i = 0; i <= 3; i++) {
if (collide(i)) {
console.log("I am colliding with something");
if (typeof getCollideIndx === 'undefined') {
console.log("collide index is not undefined");
if (!getCollideIndx(i)) {
console.log("Made it past null check");
//...update object based on collision
的碰撞()函数是:
function collide(b) {
for (i = 0; i <= 3; i++) {
//Distance between each object
var distance = (Math.sqrt(Math.pow((balls[b].x - balls[i].x), 2) + Math.pow(balls[b].y - balls[i].y, 2)));
if (distance < 32) {
//must be less than 2*radius -- all radii are the same
//makes it so that it doesn't return true when checking its own index
if (!(balls[b].mass == balls[i].mass)) {
return true;
} else {
return false;
}
}
}
}
我没有看到任何有无限循环,什么叫你的代码的第一个块?如果你在第一个'for'循环的上面打印一些东西,它是否也打印了一吨? – loganfsmyth
这是否属于某种运行循环?我猜你的循环运行速度可能更快,例如30 fps。如果是这样,那么你会每秒拨打这个代码30次? –
它在游戏的循环函数内每30毫秒调用一次。每当我运行它时,它每秒打印15,000次以上,页面冻结。这里有一个链接的网页:univeloper.freeiz.com/simulation.html – ZacB