Chrisir is right to suggest using the system timer when developing your timer class. Using a simple counter based on frame rate will mean that the program will vary from computer to computer.
Also the timer class should only be responsible for keeping track of time, in your code the timer class is initiating actions (e.g. advance, check and destroy) so this timer class can only be used in the current sketch. A timer class that just tracks time could be used in any sketch.
Below is the code for a high resolution timer class (StopWatch) that can be used in any sketch. To demonstrate its use I have created a sketch which has a tank that follows the mouse and fires a shell when the mouse button is clicked (after a short interval to reload).
CLICK HERE TO SEE THE SKETCH
The sketch is called TankDemo and has 3 classes (each on its own tab)
Shell this is responsible for moving and drawing itself once it has been fired
Tank this is responsible for moving the tank so that it follows the mouse, drawing the tank and firing a shell
StopWatch this is the high resolution timer used to measure time intervals.
The final tab (TankDemo) is the main sketch code which has overall control.
Notice that each class has clearly defined responsibilities and that these are appropriate for the class. By appropriate I mean that the tank fires its gun not the timer so the Tank class has the fire gun method.
Also notice in the main draw method (in TankDemo) the first thing that happens is that the elapsed time is retrieved from the stop watch and this time is passed to the update method for the shell and tank. Once updated both the shell and the tank are told to draw themselves.
Shell.pde
- class Shell {
- final float SPEED = 600;
- float px, py; // shell's position
- float vx, vy; // shell's velocity
- float timeLeftToFly = 0;
- boolean inFlight = false;
- void initShell(Tank t) {
- // Start the shell at the same position of the tank
- px = t.px;
- py = t.py;
- // Make sure the shell fires in the same direction
- // as the tank turret
- vx = SPEED * cos(t.angle);
- vy = SPEED * sin(t.angle);
- // Set the shell's flight time
- timeLeftToFly = t.RELOAD_TIME;
- }
- void update(float eTime) {
- px += vx * eTime;
- py += vy * eTime;
- timeLeftToFly -= eTime;
- }
- void draw() {
- // Only draw shell if there is still some time to fly
- if (timeLeftToFly > 0) {
- pushStyle(); // Save current drawing style
- // Initialise drawing style
- ellipseMode(CENTER);
- noStroke();
- fill(64, 64, 200);
- // Save current transformation matrix
- pushMatrix();
- translate(px,py);
- ellipse(0, 0, 6, 6);
- popMatrix();
- popStyle();
- }
- }
- }
Tank.pde
- class Tank {
- final float SPEED = 50;
- final float RELOAD_TIME = 1.1;
- float px, py; // tank's position
- float vx, vy; // tank's velocity
- float angle; // Angle tank is facing (radians)
- float timeToReload;
- Tank() {
- px = width/2;
- py = height/2;
- }
-
- void update(float eTime) {
- float distance = dist(mouseX, mouseY, px, py);
- if (distance > 4) {
- vx = SPEED * (mouseX - px) / distance;
- vy = SPEED * (mouseY - py) / distance;
- angle = atan2(vy, vx); // direction tank is facing
- }
- else {
- vx = 0;
- vy = 0;
- }
- px += vx * eTime;
- py += vy * eTime;
- timeToReload -= eTime;
- }
- void draw() {
- // Save current drawing style
- pushStyle();
- // Initialise drawing style
- ellipseMode(CENTER);
- rectMode(CORNER);
- stroke(0);
- strokeWeight(1.1);
- // Save current transformation matrix
- pushMatrix();
- translate(px, py);
- rotate(angle);
- fill(200, 64, 64);
- rect(-14, -14, 40, 28);
- fill(200, 100, 100);
- rect(14, -3, 30, 6);
- ellipse(0, 0, 32, 22);
- popMatrix();
- popStyle();
- }
- void fireGun() {
- if (timeToReload < 0) {
- timeToReload = RELOAD_TIME;
- shell.initShell(this);
- }
- }
- }
StopWatch.pde
- public class StopWatch {
- private long currTime;
- private long lastTime;
- private long lapTime;
- private long startTime;
- public StopWatch() {
- reset();
- }
- // Reset the timer to zero time
- public void reset() {
- currTime = lastTime = startTime = System.nanoTime();
- lapTime = 0;
- }
- // Get the time (seconds) since the stop watch was reset
- public double getRunTime() {
- double rt = 1.0E-9 * (System.nanoTime() - startTime);
- return rt;
- }
- // Get the time elapsed (seconds) since last call to
- // this method
- public double getElapsedTime() {
- currTime = System.nanoTime();
- lapTime = currTime - lastTime;
- lastTime = currTime;
- return 1.0E-9 * lapTime;
- }
- }
TankDemo.pde
- StopWatch sw;
- float elapsedTime;
- Tank tank;
- Shell shell;
- void setup(){
- size(600,600);
- cursor(CROSS);
- tank = new Tank();
- shell = new Shell();
- sw = new StopWatch(); // Starts at time zero
- }
- void draw(){
- background(0,200,0);
- fill(0);
- text("Click the mouse button to fire", 20, 30);
- // Get the time since the last call to draw
- elapsedTime = (float) sw.getElapsedTime();
- // get the shell and the tank to update their
- // positions based on the elapsed time
- shell.update(elapsedTime);
- tank.update(elapsedTime);
- // Get the shell and the tank to draw themselves
- shell.draw();
- tank.draw();
- }
- void mouseClicked(){
- tank.fireGun();
- }