Javascript 检查bullet对象是否已离开屏幕并从阵列中移除

Javascript 检查bullet对象是否已离开屏幕并从阵列中移除,javascript,p5.js,Javascript,P5.js,我有一个子弹系统,可以沿y轴创建和发射子弹(椭圆)。我想检查子弹是否已离开屏幕,以便我可以将其从子弹阵列中删除,但我不确定如何检查。函数edges()用于检查项目符号是否已离开屏幕。该类在另一个主类中引用,因此这里没有函数setup() 类公告系统{ 构造函数(){ 这个.bullets=[]; this.velocity=新的createVector(0,-5); 这个直径=10; } 运行(){ 这个。移动(); 这个.draw(); 这个。边(); } 火(x,y){ 这个.bullets

我有一个子弹系统,可以沿y轴创建和发射子弹(椭圆)。我想检查子弹是否已离开屏幕,以便我可以将其从子弹阵列中删除,但我不确定如何检查。函数edges()用于检查项目符号是否已离开屏幕。该类在另一个主类中引用,因此这里没有函数setup()

类公告系统{
构造函数(){
这个.bullets=[];
this.velocity=新的createVector(0,-5);
这个直径=10;
}
运行(){
这个。移动();
这个.draw();
这个。边();
}
火(x,y){
这个.bullets.push(createVector(x,y));
}
//抽出所有子弹
画(){
填充(255);

对于(var i=0;i,因此首先您可能希望使用
height
而不是
window.innerHeight
。变量
height
由p5js提供,并提供画布的高度,该高度可能不同于
window.innerHeight

其次,您的条件
if(this.bullets.y>window.innerHeight)
没有意义,因为
this.bullets
是一个数组:它没有
y
属性

在所有其他函数中,您迭代
this.bullets
数组以作用于每个bullet对象:在这里,您希望执行相同的操作,例如:

//check if bullets leave the screen and remove them from the array
edges(){
  let i=this.bullets.length - 1;
  while (i>=0) {
      if(this.bullets[i].y > height){
          this.bullets.splice(i, 1);  
      }
      i--;
  }
}

这将从末尾开始迭代数组,对于每个索引检查项目符号是否仍在画布中,如果需要,将其移除,并减少下一个索引
i
,以进行评估。

因此,首先您可能希望使用
高度
而不是
窗口。innerHeight
。变量
高度
由p5提供js并提供画布的高度,该高度可能不同于
window.innerHeight

其次,您的条件
if(this.bullets.y>window.innerHeight)
没有意义,因为
this.bullets
是一个数组:它没有
y
属性

在所有其他函数中,您迭代
this.bullets
数组以作用于每个bullet对象:在这里,您希望执行相同的操作,例如:

//check if bullets leave the screen and remove them from the array
edges(){
  let i=this.bullets.length - 1;
  while (i>=0) {
      if(this.bullets[i].y > height){
          this.bullets.splice(i, 1);  
      }
      i--;
  }
}

这将从末尾开始迭代数组,对于每个索引检查项目符号是否仍在画布中,如果需要,将其移除,并减少下一个索引
i
,以进行评估。

以下是您可以参考的版本:

let bull = [];
let obs;

function setup() {
  createCanvas(400, 400);
 obs = 300;
}

function draw() {
  background(220);
  
  line(0,obs,width,obs);
  
  for (i = 0; i < bull.length; i++) {
    bull[i].show();
    bull[i].move();    
    if(bull[i].y < 0 || bull[i].y + bull[i].w>= obs)
      bull.splice(i,1);
  }
}

function keyTyped() {
  append(bull, new Bullets(mouseX, mouseY, 0, 1));
}


class Bullets {

  constructor(x, y, xdir, ydir) {
    this.x = x;
    this.y = y;
    this.w = 10;
    this.xdir = xdir;
    this.ydir = ydir;
    this.speed = 3;
  }

  show() {
    ellipse(this.x, this.y, this.w, this.w);
  }

  move() {
    this.x += this.xdir * this.speed;
    this.y += this.ydir * this.speed;
  }
}
let bull=[];
让obs;
函数设置(){
createCanvas(400400);
obs=300;
}
函数绘图(){
背景(220);
行(0,obs,宽度,obs);
对于(i=0;i=obs)
接头(i,1);
}
}
函数keyTyped(){
追加(bull,新项目符号(mouseX,mouseY,0,1));
}
类子弹{
构造函数(x、y、xdir、ydir){
这个.x=x;
这个。y=y;
这是w=10;
this.xdir=xdir;
this.ydir=ydir;
这个速度=3;
}
show(){
椭圆(this.x,this.y,this.w,this.w);
}
移动(){
this.x+=this.xdir*this.speed;
this.y+=this.ydir*this.speed;
}
}

子弹击中障碍物后,将从阵列中移除,在本例中,障碍物就是线。

以下是您可以参考的版本:

let bull = [];
let obs;

function setup() {
  createCanvas(400, 400);
 obs = 300;
}

function draw() {
  background(220);
  
  line(0,obs,width,obs);
  
  for (i = 0; i < bull.length; i++) {
    bull[i].show();
    bull[i].move();    
    if(bull[i].y < 0 || bull[i].y + bull[i].w>= obs)
      bull.splice(i,1);
  }
}

function keyTyped() {
  append(bull, new Bullets(mouseX, mouseY, 0, 1));
}


class Bullets {

  constructor(x, y, xdir, ydir) {
    this.x = x;
    this.y = y;
    this.w = 10;
    this.xdir = xdir;
    this.ydir = ydir;
    this.speed = 3;
  }

  show() {
    ellipse(this.x, this.y, this.w, this.w);
  }

  move() {
    this.x += this.xdir * this.speed;
    this.y += this.ydir * this.speed;
  }
}
let bull=[];
让obs;
函数设置(){
createCanvas(400400);
obs=300;
}
函数绘图(){
背景(220);
行(0,obs,宽度,obs);
对于(i=0;i=obs)
接头(i,1);
}
}
函数keyTyped(){
追加(bull,新项目符号(mouseX,mouseY,0,1));
}
类子弹{
构造函数(x、y、xdir、ydir){
这个.x=x;
这个。y=y;
这是w=10;
this.xdir=xdir;
this.ydir=ydir;
这个速度=3;
}
show(){
椭圆(this.x,this.y,this.w,this.w);
}
移动(){
this.x+=this.xdir*this.speed;
this.y+=this.ydir*this.speed;
}
}

子弹击中障碍物后,将从阵列中移除,在本例中,这是一条线。

我尝试了此操作,程序在调用bulletsystem时停止。您会收到什么错误消息?这条线也是
this.velocity=new createVector(0,-5);
可能是错误的,因为
createVector
不是您可能想要的类
this.velocity=createVector(0,-5)
未捕获类型错误:无法读取BulletSystem.edges(BulletSystem.js:38)处BulletSystem.run(BulletSystem.js:12)处Spaceship.run(Spaceship.js:13)处绘制时未定义的属性“y”(sketch.js:27)在b.a.default.redraw(p5.min.js:3)在(u draw(p5.min.js:3))哦,事实上我在初始化
I
时忘记了
-1
)它似乎没有从数组中删除项目符号,或者没有检测到项目符号移出屏幕。我尝试了此操作,程序在调用bulletsystem时停止。您收到了什么错误消息?还有这行
this.velocity=new createVector(0,-5);
可能是错误的,因为
createVector
不是您可能想要的类
this.velocity=createVector(0,-5)
未捕获类型错误:无法读取BulletSystem.edges(BulletSystem.js:38)处BulletSystem.run(BulletSystem.js:12)处Spaceship.run(Spaceship.js:13)处绘制时未定义的属性“y”(sketch.js:27)在b.a.default.redraw(p5.min.js:3)在_draw(p5.min.js:3)哦,事实上我在初始化
I
:)时忘记了一个
-1
),它似乎没有从数组中移除子弹,或者没有检测到子弹移出屏幕。