Timer that ends with a song

Hi,

I’m very new to processing. Right now I’m trying to create a game for kids. The thing is that I’m trying to create a timer and when it is equal to 0 a song will go off. So thanks to this forum and Youtube I manage to create a code that now plays the music (unfortunately the song goes off right away) and my timer counts down from 10, tho it doesn’t stop at 0. Do you know what I should do?

Klocka

Timer startTimer;

import ddf.minim.*;

AudioPlayer player;
Minim minim;//audio context

void setup()
{
  size(600, 600);
  background(255);
  startTimer = new Timer(10); 
  
  minim = new Minim(this);
  player = minim.loadFile("old.mp3", 2048);
  player.play();

}

void draw()
{
  background(200);
  startTimer.countDown();
  fill(0);
  text(startTimer.getTime(),20,20);
}
  
void stop()
{
  player.close();
  minim.stop();
  super.stop();
}

**Timer**

class Timer 
{
  float Time;
  Timer(float set) 
  {
    Time = set;
  }
  float getTime() 
  {
    return(Time);
  }
  void setTimer(float set) 
  {
    Time = set;
  }
  
  void countDown() 
  {
    Time -= 1/frameRate;
  }

}
1 Like

Hey,

You can format your code by :

  • Pressing Ctrl+T in the Processing IDE code editor.
  • Using the button </> in the message editor on this forum.

Rather than playing the player as soon as the sketch runs, play it in draw loop when the timer reaches 0.

if(startTimer.getTime() <= 0){ 
    player.play();
  }

Only decrease the value of the timer if it is greater than 0. (And if it tries to go to less than 0 snap it back to 0.)

if(Time > 0){
      Time -= 1/frameRate;
      if(Time < 0) Time = 0;
    }
}
1 Like

Thank you! It now works :slight_smile:

1 Like

Thank you for the tip! :slight_smile: