繁体   English   中英

如何在 if 语句中运行 forEach

[英]How to run a forEach in an if statement

如何在 if 语句中运行 forEach? 如果有任何子弹通过他们,我正在尝试为每个敌人运行 function。 我已经尝试过了,但没有奏效:

bullets.forEach(b => {
  if(b.y < this.y){
    var index = this.parent.indexOf(this);
    this.parent.splice(index, 1);
    this.img.remove();
    this.parent.push(new Enemy(rng(2),rng(2),enemy));
  }
});

和这个:

if (bullets.forEach().y > this.y) {
  var index = this.parent.indexOf(this);    
  this.parent.splice(index, 1);
  this.img.remove();
  this.parent.push(new Enemy(rng(2),rng(2),enemy));
}

此代码位于 Enemy() 构造函数中。

您的 forEach 循环已正确编写,一定还有其他不起作用的东西。 您可以创建一个较小的测试问题并单独测试,也可以在此处发布测试版本问题。 问题需要更清楚。

暂无
暂无

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

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