Why does only p2 show up?

Hi Iā€™m very new to programming so excuse if this is an easy question, but why is only one of my particles showing up (p2)?

Thanks in advance :slight_smile:

Particle p1;
Particle p2;

void setup() {
  size(600,400);
  p1 = new Particle(300,200,80);
  p2 = new Particle(70,70,100);
}

void draw() {
  background(0);
  p1.display();
  p2.display();
}

float x, y;
float r;

class Particle {
  
  Particle(float tempX, float tempY, float tempR) {
    x = tempX;
    y = tempY;
    r = tempR;
  }
  
  void display() {
    fill(255,0,0);
    ellipse(x,y,r*2,r*2);  
  }
  
}
1 Like

Hi, your variables x, y and r are globals.
You need to put them inside the class, that way each Particle will be able to store its own values instead of looking for global values.

class Particle {
  
  float x, y, r;
  
  Particle(float tempX, float tempY, float tempR) {
    x = tempX;
    y = tempY;
    r = tempR;
  }
  
  void display() {
    fill(255,0,0);
    ellipse(x,y,r*2,r*2);  
  }
  
}

Take a look at this:
https://processing.org/tutorials/objects/

4 Likes

Hi there, thanks very much for the quick reply, helped a lot. :grin:

1 Like