Create a simple countdown in processing

D34thSt4lker picture D34thSt4lker · Sep 14, 2012 · Viewed 45.1k times · Source

I have searched up so many sites on Google to try and get this to work but NO ONE seems to have this anywhere , and if they do it's just NOT working with my program... What I am trying to achieve is to have a player recoil that when the player gets hit, he has a "x" amount of time between getting hit the first time and the second time.

So I have a Boolean "hit" = false and when he gets hit, it changes to true. Which means he can't get hit again until it's changed to false again.

So I'm trying to set up a function in my program to set a "timer" for "x" amount of seconds IF hit = true and once that timer hits "x" amount of seconds, hit will get switched back to false.

Anyone have any ideas?

Thanks!!

Answer

George Profenza picture George Profenza · Sep 14, 2012

A simple option is to manually keep track of time using millis().

You would use two variables:

  1. one to store elapsed time
  2. one to store the wait/delay time you need

In the draw() method you would check if the difference between the current time (in millis.) and the previously stored time is greater(or equal) to the delay.

If so, this would the you're cue to do whatever for the delay given and update the stored time:

int time;
int wait = 1000;

void setup(){
  time = millis();//store the current time
}
void draw(){
  //check the difference between now and the previously stored time is greater than the wait interval
  if(millis() - time >= wait){
    println("tick");//if it is, do something
    time = millis();//also update the stored time
  }
}

Here's a slight variation the updates a 'needle' on screen:

int time;
int wait = 1000;

boolean tick;

void setup(){
  time = millis();//store the current time
  smooth();
  strokeWeight(3);
}
void draw(){
  //check the difference between now and the previously stored time is greater than the wait interval
  if(millis() - time >= wait){
    tick = !tick;//if it is, do something
    time = millis();//also update the stored time
  }
  //draw a visual cue
  background(255);
  line(50,10,tick ? 10 : 90,90);
}

Depending on your setup/needs, you may choose to wrap something like this into a class that can be reused. This is a basic approach and should work with the Android and JavaScript versions as well (although in javascript you've got setInterval()).

If you're interested in using Java's utilities, as FrankieTheKneeMan suggested, there is a TimerTask class available and I'm sure there are plenty of resources/examples out there.

You can run a demo bellow:

var time;
var wait = 1000;

var tick = false;

function setup(){
  time = millis();//store the current time
  smooth();
  strokeWeight(3);
}
function draw(){
  //check the difference between now and the previously stored time is greater than the wait interval
  if(millis() - time >= wait){
    tick = !tick;//if it is, do something
    time = millis();//also update the stored time
  }
  //draw a visual cue
  background(255);
  line(50,10,tick ? 10 : 90,90);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.4/p5.min.js"></script>

Update There many ways to make timers. Here's a version that uses a Thread and calls a functioned defined in the sketch by name. This is not a simple countdown. Straight up millis() as explained above is simple enough, just not very flexible:

Timer timer;

void setup(){
  noStroke();

  //textSize(12);
  timer = new Timer(this,"onTimerTick","onTimerComplete");
  // start a timer for 10 seconds (10 * 1000 ms) with a tick every second (1000 ms) 
  timer.reset(10 * 1000,1000);
}

void draw(){
  background(0);
  drawTimer();
  //rect(0,0,timer.progress * width,height);
  //blendMode(DIFFERENCE);
  text("'1' = reset"+
     "\n'2' = cancel"+
     "\n'3' = pause"+
     "\n'4' = resume"+
     "\n"+(int)(timer.progress * 100)+"%",10,15);
}

void drawTimer(){
  pushStyle();
  noFill();
  stroke(255);
  strokeWeight(3);
  ellipse(450, 54,90,90);
  fill(192,0,0);
  noStroke();
  pushMatrix();
  translate(50,50);
  rotate(radians(-90));
  arc(0, 0, 90, 90, 0, timer.progress * TWO_PI, PIE);
  popMatrix();
  popStyle();
}

void keyPressed(){
  if(key == '1'){
    timer.reset(3000,10);
  }
  if(key == '2'){
    timer.cancel();
  }
  if(key == '3'){
    timer.pause();
  }
  if(key == '4'){
    timer.resume();
  }
}

public void onTimerTick(){
  println("tick",(int)(timer.progress * 100),"%");
}

public void onTimerComplete(){
  println("complete");
}

import java.lang.reflect.Method;
// utility timer class
class Timer implements Runnable{
  // is the timer still ticking or on hold ?
  boolean isPaused = false;
  // is the thread still running ?
  boolean isRunning = true;

  // how close are we to completion (0.0 = 0 %, 1.0 = 100%)
  float progress = 0.0;
  // a reference to the time in ms since the start of the timer or reset
  long now;
  // default duration
  long duration = 10000;
  // default tick interval
  long tickInterval = 1000;
  // time at pause
  long pauseTime;

  // reference to the main sketch
  PApplet parent;
  // function to call on each tick
  Method onTick;
  // function to call when timer has completed
  Method onComplete;

  Timer(PApplet parent,String onTickFunctionName,String onCompleteFunctionName){
    this.parent = parent;
    // try to store a reference to the tick function based on its name
    try{
      onTick = parent.getClass().getMethod(onTickFunctionName);
    }catch(Exception e){
      e.printStackTrace();
    }

    // try to store a reference to the complete function based on its name
    try{
      onComplete = parent.getClass().getMethod(onCompleteFunctionName);
    }catch(Exception e){
      e.printStackTrace();
    }
    // auto-pause
    isPaused = true;
    // get millis since the start of the program
    now = System.currentTimeMillis();
    // start the thread (processes run())
    new Thread(this).start();
  }

  // start a new stop watch with new settings
  void reset(long newDuration,long newInterval){
    duration = newDuration;
    tickInterval = newInterval;
    now = System.currentTimeMillis();
    progress = 0;
    isPaused = false;
    println("resetting for ",newDuration,"ticking every",newInterval);
  } 

  // cancel an existing timer
  void cancel(){
    isPaused = true;
    progress = 0.0;
  }

  // stop this thread
  void stop(){
    isRunning = false;
  }

  void pause(){
    isPaused = true;
    pauseTime = (System.currentTimeMillis() - now); 
  }
  void resume(){
    now = System.currentTimeMillis() - pauseTime;
    isPaused = false;
  }

  public void run(){
    while(isRunning){

      try{
          //sleep per tick interval
          Thread.sleep(tickInterval);
          // if we're still going
          if(!isPaused){
            // get the current millis
            final long millis = System.currentTimeMillis();
            // update how far we're into this duration
            progress = ((millis - now) / (float)duration);
            // call the tick function
            if(onTick != null){
              try{
                onTick.invoke(parent);
              }catch(Exception e){
                e.printStackTrace();
              }
            }
            // if we've made, pause the timer and call on complete
            if(progress >= 1.0){
              isPaused = true;
              // call on complete
              if(onComplete != null){
              try{
                  onComplete.invoke(parent);
                }catch(Exception e){
                  e.printStackTrace();
                }
              }
            }
          }
        }catch(InterruptedException e){
          println(e.getMessage());
        }
      }
    }

}

Threaded Timer demo sketch preview: an arc turning into a circle based on progress through time

Additionally you can use Java's TimerTask class.