所以我正在使用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