using class to make timer
in
Programming Questions
•
11 months ago
mostly having trouble in the getSecondsLeft method inside the timer class. It works find until I go over 60 seconds time. I cant think of a way to reset the seconds timer. There is also the problem that it runs several times a second and minute timer has several minutes taken out because it passes conditional a few times. I can solve this but doing a reference check to see if the seconds value changes, but this seems messy...?
also thought about using second() but not sure if this would be best approach.
any suggestions are much appreciate.
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.event.*;
- import java.awt.*;
- import java.awt.PointerInfo.*;
- Timer timer;
- PFont font;
- PFont font2;
- PImage bg;
- int posX;
- int posY;
- void setup(){
- frame.setResizable(true);
- font = loadFont("AvantGarde-ExtraLight-68.vlw");
- font2 = loadFont("AvantGarde-ExtraLight-44.vlw");
- size(362, 131);
- timer = new Timer(25);
- timer.start();
- //load assets
- bg = loadImage("bg.png");
- }
- void draw() {
- // draw background
- image(bg, 0, 0);
- // timer
- drawMinutes();
- drawSeconds();
- // start, restart
- fill(18, 146, 220);
- text("START", 210, 85);
- if (timer.isFinished()){
- // do something
- }
- }
- void drawMinutes(){
- textFont(font);
- fill(88, 88, 88);
- text(timer.getMinutesLeft(), 50, 85 );
- }
- void drawSeconds(){
- textFont(font2);
- int secY = 85;
- int secX = 130;
- fill(35, 35, 35);
- text(timer.getSecondsLeft(), secX, secY-1);
- }
class file with problem:
- class Timer {
- //seconds
- int lastMillis;
- int startTimeInSeconds;
- int timePassed;
- int secondsLeft;
- //minutes
- int inputTime;
- int minutesLeft;
- Timer(int tempTotalTime){
- inputTime = tempTotalTime; //input in MINUTES
- }
- void start() {
- minutesLeft = inputTime;
- }
- int getMinutesLeft(){
- return minutesLeft;
- }
- int getSecondsLeft(){
- timePassed = millis()/1000;
- println("passed: "+timePassed);
- secondsLeft = 60 - timePassed;
- println("sec left: "+secondsLeft);
- if(timePassed % 60 == 0){
- minutesLeft -= 1;
- }
- return secondsLeft;
- }
- boolean isFinished() {
- if (minutesLeft == 0) {
- return true;
- } else { return false; }
- }
- }
1