Wednesday, October 9, 2013

Timer Example

OpenProcessing Example by Daniel Shiffman

_________________________________________________________________________________
CODE:


// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// example 10-4: Implementing a timer

int savedTime;
int totalTime = 5000;

void setup() {
  size(200,200);
  background(0);
  savedTime = millis();
}

void draw() {
  // Calculate how much time has passed
  int passedTime = millis() - savedTime;
  // Has five seconds passed?
  if (passedTime > totalTime) {
    println( " 5 seconds have passed! " );
    background(random(255)); // Color a new background
    savedTime = millis(); // Save the current time to restart the timer!
  }
}


_________________________________________________________________________________

I implemented this code in my Blood of Brains game to only allow the bloodbags and zombies to move once every 800 milliseconds (0.8 seconds). This code was helpful because it helped me avoid changing the FrameRate, which will often cause problems.

No comments:

Post a Comment