The Particle() is undefined

import java.util.Iterator;

ParticleSystem ps;
Particle p;

void steup(){
  size(200,200);
  ps = new ParticleSystem();
}
void draw(){
  background(255);
  ps.run();
}

class ParticleSystem{
  ArrayList<Particle> particles;
  
  ParticleSystem(){
    particles = new ArrayList<Particle>();
  }
  
  void addParticle(){
    particles.add(new Particle());
  }
  void run(){
    Iterator<Particle>it = particles.iterator();
    while(it.hasNext()){
      Particle p = it.next();
      p.run();
      if(p.isDead()){
        it.remove();
      }
    }
  }
}

class Particle{
  PVector location;
  PVector velocity;
  PVector acceleration;
  float lifespan;
  
  Particle(PVector l){
    acceleration = new PVector(0,0.05);
    velocity = new PVector(random(-1,1),random(-2,0));
    location = l.copy();
    lifespan = 255.0;
  }

  void run(){
    update();
    display();
  }
  
  void update(){
    velocity.add(acceleration);
    location.add(velocity);
    lifespan -= 2.0;
  }
  
  void display(){
    stroke(0,lifespan);
    fill(0,lifespan);
    ellipse(location.x,location.y,8,8);
  }
  
  boolean isDead(){
    if(lifespan < 0.0){
      return true;
    }else{
      return false;
    }
  }
}

Yes it is undefined. You have defined Particle constructor with PVector parameter Particle(PVector l){, but you haven’t defined constructor without parameters Particle(){ as you try to use it.

3 Likes

OH.I was negligent about this and your help allowed me to solve the error in an instant.very thanks :slightly_smiling_face:

A little problem arose and I asked you again.

You have misspelled void setup() and ps is not created before you call it.

1 Like

But how do I create it :slightly_smiling_face: :thinking:

import java.util.Iterator;

ParticleSystem ps;
Particle p;

void setup(){
  size(400,400);
  ps = new ParticleSystem();
}
void draw(){
  background(255);
  ps.run();
}

class ParticleSystem{
  ArrayList<Particle> particles;
  
  ParticleSystem(){
    particles = new ArrayList<Particle>();
  }
  
  void addParticle(){
    particles.add(new Particle());
  }
  void run(){
    Iterator<Particle>it = particles.iterator();
    while(it.hasNext()){
      Particle p = it.next();
      p.run();
      if(p.isDead()){
        it.remove();
      }
    }
  }
}

class Particle{
  PVector location;
  PVector velocity;
  PVector acceleration;
  float lifespan;
  
  Particle(){
    acceleration = new PVector(0,0.05);
    velocity = new PVector(random(-1,1),random(-2,0));
    location = location.copy();
    lifespan = 255.0;
  }

  void run(){
    update();
    display();
  }
  
  void update(){
    velocity.add(acceleration);
    location.add(velocity);
    lifespan -= 2.0;
  }
  
  void display(){
    stroke(0,lifespan);
    fill(0,lifespan);
    ellipse(location.x,location.y,8,8);
  }
  
  boolean isDead(){
    if(lifespan < 0.0){
      return true;
    }else{
      return false;
    }
  }
}

This creates an empty storage for Particles. To get something going on you need to add Particles the storage. You can add Particle with command ps.addParticle()

1 Like

yea very thankyou,I learning :slightly_smiling_face:

1 Like