How to make an animation appear for multiple seconds when collison happens?

Hello my name is Julio and i’m an undergraduate student that is new to processing.

My question is about how can i make a variable inside of a collision appear for multiple seconds.

The code part i need help with is the following, I underlined the part that i need to happen for multiple seconds:

  if (missile2.x >= paddle2.x && missile2.x <= paddle2.x + paddle2.w && missile2.y >= paddle2.y && missile2.y <= paddle2.y + paddle2.h)
  {
    **image( up[frameCount%14], 300, 400 );**
    lives += 1;
  }
1 Like

Hello and welcome to this forum!

Great to have you here.

instead of using image( up[frameCount%14], 300, 400 ); inside the if, set a boolean flag explosionIsOn (or whatever name you choose) to true there and evaluate explosionIsOn separately in draw() and show the explosion there.

And you can use a timer to end the explosion

Chrisir

boolean explosionIsOn = false;
int timer; 

void setup() {
  size(1200, 600);
  background(0);
}

void draw() {
  background(0); 

  if (explosionIsOn) {
    text("explosion", 211, 211);
    if (millis() - timer > 930) {   // end explosion
      explosionIsOn = false;
    }
  }
}

void mousePressed() {
  explosionIsOn = true;   // Start explosion
  timer=millis();
}
1 Like