簡體   English   中英

js游戲凍結,沒有錯誤

[英]js game freezes with no error

我正在開發一個涉及構建,破壞和生存的JavaScript游戲。 它一直運行良好,但是在添加樹木后,游戲會在打破障礙后隨機凍結。 代碼在這里:

for (var bl in blocks) {
           if (mouse.x >= blocks[bl].x-camera.x && mouse.y >= camera.y+blocks[bl].y && mouse.x <= blocks[bl].x-camera.x+64 && mouse.y <= camera.y+blocks[bl].y+64) {
                document.body.style.cursor = "pointer";
                if (mouse.down) {
                     if (!blocks[bl].d && blocks[bl].d !== 0) {
                          blocks[bl].d = 32;
                     } else if (blocks[bl].d > 0) {
                          blocks[bl].d -= 0.5;
                          if (tools[player.tool].n === 'axe') {
                               blocks[bl].d -= 1;
                          }
                     } else {
                          var fb = false;
                          for (var i in inventory) {
                               if (inventory[i].n === blocks[bl].n) {
                                    inventory[i].a ++;
                                    fb = true;
                               }
                          }
                          if (!fb) {
                               inventory.push({n: blocks[bl].n, a: 1});
                          }
                          blocks.splice(bl, 1);
                     }
                }
           }
      }

我看不到有無限循環的可能,並且在發生任何錯誤時都不會顯示任何錯誤。

編輯

我將代碼更改為

          var spliceblock = {bl: 0, s: false};
          for (var bl in blocks) {
               if (mouse.x >= blocks[bl].x-camera.x && mouse.y >= camera.y+blocks[bl].y && mouse.x <= blocks[bl].x-camera.x+64 && mouse.y <= camera.y+blocks[bl].y+64) {
                    document.body.style.cursor = "pointer";
                    if (mouse.down) {
                         if (!blocks[bl].d && blocks[bl].d !== 0) {
                              blocks[bl].d = 32;
                         } else if (blocks[bl].d > 0) {
                              blocks[bl].d -= 0.5;
                              if (tools[player.tool].n === 'axe') {
                                   blocks[bl].d -= 1;
                              }
                         } else {
                              var fb = false;
                              for (var i in inventory) {
                                   if (inventory[i].n === blocks[bl].n) {
                                        inventory[i].a ++;
                                        fb = true;
                                   }
                              }
                              if (!fb) {
                                   inventory.push({n: blocks[bl].n, a: 1});
                              }
                              spliceblock.s = true;
                              spliceblock.bl = bl;
                              //blocks.splice(bl, 1);
                         }
                    }
               }
          }
          if (spliceblock.s) {
               blocks.splice(spliceblock.bl, 1);
          }

但是當嘗試打破障礙時,它仍然會隨機凍結。

在遍歷數組時修改數組(使用拼接)勢必會引起問題。 如果從陣列中刪除塊bl,然后繼續運行它,則計數器可能會關閉。

相反,存儲要刪除的塊的索引,然后在循環瀏覽完塊之后將其刪除。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM