Label is not updated inside a for loop

I try to update a label inside a for loop but it is not updated until the loop ends, I use processin 4.3 G4P GUI Builder 4.4.2 on linux Mint
my code is the following

public void button1_click1(GButton source, GEvent event) { //_CODE_:button1:431015:
  if(event == GEvent.CLICKED){
  for (int a =1; a<=20;a++){
    String b = str(a);
    label1.setText(b);
    label1.forceBufferUpdate();
    delay (500);
  }
  }
} //_CODE_:button1:431015:

Could someone explain to me what I’m doing wrong?

1 Like

no mistake on your side

the screen is not updated throughout but only once at the end of draw(). Big Processing principle.

this goes for all animations and stuff: don’t use for-loops but the fact that draw loops automatically.

You can replace the for-loop with a structure in draw().

Chrisir :slight_smile:

Example


int counter = 0;

void setup() {
  size(700, 700);
}

void draw() {
  background(0);

  if (counter<=20) {
    String textOutput = str(counter);
    text(textOutput, 22, 22);
    delay (500);

    counter++;
  } else {
    text("Loop is over.", 22, 22);
  }//else
  //
} //

1 Like