Object Methods not looping

#1

Hi there,

Not sure if this a bug or if I have done something silly, but I have a very simple sketch using a class which for some reason is not updating/looping…

Circle ball;


void setup() {
  size (100,100); 
  background(255);
  ball = new Circle();
  
}

void draw() {
      pushMatrix();
      translate (width/2,height/2);
        ball = new Circle();
        ball.display();
      popMatrix();
      
}

class Circle { //<----------- Class
float d =10;
float grow = 1;


//constructor
Circle(){  
}
    
void display() {
  fill(255,0,0);
  ellipse(0,0,d,d);
    
  d= d + grow;
  if (d < 0 || d > 60) {
  grow = grow * -1;
  }
 }
}

1 Like
#2

you only need to instantiate the ball in the setup atm you are re instantiating the ball every draw so change this

void draw() {
  pushMatrix();
  translate (width/2, height/2);
  ball = new Circle();
  ball.display();
  popMatrix();
}

to

void draw() {
  background(0);
  pushMatrix();
  translate (width/2, height/2);
  ball.display();
  popMatrix();
}

and you should be right.

3 Likes