简体   繁体   English

如何从游戏循环内的数组中删除 object (setInterval)

[英]How to remove an object from an array inside a game loop (setInterval)

I have a click event which checks if the object in the array should be removed(isSquashed) and when it's true we remove the object from the list of array but when that condition comes I either need to break out of the loop or decrement the i value, basically I want to come out of the for loop and call the gameloop again with the newArray list length after splicing the array object我有一个点击事件,它检查数组中的 object 是否应该被删除(isSquashed),当它是真的时,我们从数组列表中删除 object 但是当这种情况出现时,我需要跳出循环或减少i值,基本上是想跳出for循环,拼接数组object后用newArray列表长度再次调用gameloop

I tried both ways我尝试了两种方式

  1. iterating the for loop backward向后迭代for循环
  2. giving the break statement(getting illegal break Statement) but it's still not happening for me so could someone help me out with this and let me know how I could possibly fix this,给出中断声明(获取非法中断声明),但它仍然没有发生在我身上,所以有人可以帮我解决这个问题,让我知道如何解决这个问题,

     maingameloop = function(antsArray) { //inititialization // antsArray[i].draw(); // antsArray[i].checkifSmashed(); //gameloop if (this.isplaying) { console.log(this.score); for (let i = 0; i < antsArray.length; i++) { let gameloop = setInterval(() => { antsArray[i].move(); antsArray[i].update(antsArray); if (antsArray[i].isSquashed) { this.score++; antsArray.splice(i, 1); // i--; clearInterval(gameloop); // this.isplaying = false; } }, this.FRAME_RATE, ); } } else { //gameover // this.maingameloop(antsArray); } }

the o/p I'm getting when I remove the object from the array is:当我从数组中删除 object 时得到的 o/p 是:

Uncaught TypeError: Cannot read property 'move' of undefined

Never a good idea to loop intervals or timeouts循环间隔或超时绝不是一个好主意

Try this尝试这个

var cnt = antsArray.length

function ants() {
  if (cnt <= 0) {
    //gameover
    // this.maingameloop(antsArray);
    return;
  }
  if (this.isplaying) {
    console.log(this.score);
    antsArray[cnt].move();
    antsArray[cnt].update(antsArray); // ???
    if (antsArray[cnt].isSquashed) {
      this.score++;
      cnt--;
    }
    setTimeout(play, this.FRAME_RATE);
  }
}

Avoid setInterval inside a loop because it would require an asynchronous callback to run inside a synchronous loop which will always show these kinds of errors.避免在循环中使用 setInterval,因为它需要异步回调才能在同步循环中运行,而同步循环总是会显示这些类型的错误。

i cleared the setInterval and then i checked for the conditions and added a break which wouldnt be illegal since im out of the interval and so it worked out for me thanks guys for your suggestions我清除了 setInterval 然后我检查了条件并添加了一个休息时间,因为我超出了时间间隔,所以这不会是非法的,所以它对我来说很有效,谢谢大家的建议

maingameloop = function (antsArray) {主游戏循环 = function (antsArray) {

    //inititialization

    // antsArray[i].draw();
    // antsArray[i].checkifSmashed();

    //gameloop
    if (this.isplaying) {
      for (let i = 0; i< antsArray.length;  i++) {

      let gameloop = setInterval(() => {
        antsArray[i].move();
        antsArray[i].update(antsArray);

          if (antsArray[i].isSquashed) {
            this.score++;                             
            clearInterval(gameloop);           
          }

        }  
    , this.FRAME_RATE);

    if(antsArray[i].isSquashed ){
      antsArray.splice(i, 1);
     break;
    }  
    }
  }else {
      //gameover
      // this.maingameloop(antsArray);
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM