Starting a timer in an if statement?
in
Programming Questions
•
11 months ago
I'm trying to make a timer to start only if some conditions are met first however no matter what I do the timer seems to just start count from as soon as I hit run. I need this timer to start in this if statement for my game to work, any help would really help (anything involving the timer I've highlighted in red)
- // images are 720 by 540
- String[] questions = new String[4];
- int[] answers = new int[4];
- //int[] questions = new int[4];
- int numFrames = 6; // The number of frames in the animation
- int frame = 0;
- int spincount = 0;
- int state = 0;
- //questions
- int q = 0;
- // answers
- int an = 0;
- //timer
- int savedTime;
- int totalTime = 11;
- //randomise wheel start position variable
- int wheel1;
- int wheel2;
- //pixels to test
- // middle pixels
- int a;
- int b;
- int c;
- PImage[] images1 = new PImage[6];
- PImage[] images2 = new PImage[6];
- PImage[] images3 = new PImage[6];
- void setup() {
- size(1080, 720);
- frameRate(12);
- // wheel 1
- images1[0] = loadImage("bell1.png");
- images1[1] = loadImage("diamond1.png");
- images1[2] = loadImage("cherry1.png");
- images1[3] = loadImage("bell1.png");
- images1[4] = loadImage("diamond1.png");
- images1[5] = loadImage("cherry1.png");
- // wheel 3
- images3[0] = loadImage("cherry1.png");
- images3[1] = loadImage("diamond1.png");
- images3[2] = loadImage("bell1.png");
- images3[3] = loadImage("cherry1.png");
- images3[4] = loadImage("diamond1.png");
- images3[5] = loadImage("bell1.png");
- // wheel 2
- images2[0] = loadImage("bell1.png");
- images2[1] = loadImage("diamond1.png");
- images2[2] = loadImage("bell1.png");
- images2[3] = loadImage("cherry1.png");
- images2[4] = loadImage("diamond1.png");
- images2[5] = loadImage("bell1.png");
- questions[0] = "Which Apollo 11 astronaut did not set foot on the moon? A:Micheal Collins B: Collin Mitchels C: Harold Greenoff";
- questions[1] = "What two colours make up the Polish flag A:blue and white B: blue and red C: red and white?";
- questions[2] = "what is a feamle swan called? A: Cob B:Pen C:Boogle";
- questions[3] = "What is a group of Owls called? A:parliament B: murder C: Library";
- answers[0] = 1;
- answers[1] = 3;
- answers[2] = 2;
- answers[3] = 1;
- }
- void draw() {
- //background(0);
- //test state to see if I should be spinning
- if(state == 1) {
- randomstart();
- spin();
- }
- }
- //if a key is pressed then set the wheel to spin
- void keyReleased() {
- state = 1;
- }
- void randomstart() {
- wheel1 = int(random(5));
- wheel2 = int(random(5));
- //wheel3 = int(random(5));
- }
- void spin() {
- frame = (frame+1) % numFrames;
- //spin for 5 times the break out
- if (frame == 5) {
- frame = 0;
- spincount ++;
- if (spincount == 1) {
- state = 0;
- spincount = 0;
- //check if its a win
- a = images1[(frame + 1) % numFrames].get(2,2);
- b = images2[(frame + 1 + wheel1) % numFrames].get(2,2);
- c = images3[(frame + 1 + wheel2) % numFrames].get(2,2);
- winner();
- // print(a);
- // print(b);
- // print(c);
- }
- }
- // wheel 1
- image(images1[(frame) % numFrames], 0, 0);
- image(images1[(frame + 1) % numFrames], 0, 200); //this is the image to test
- image(images1[(frame + 2) % numFrames], 0, 400);
- // wheel 2
- image(images3[(frame) % numFrames], 300, 0);
- image(images3[(frame + 1 + wheel1) % numFrames], 300, 200); //this is the image to test
- image(images3[(frame + 2 + wheel1) % numFrames], 300, 400);
- // wheel 3
- image(images2[(frame) % numFrames], 600, 0);
- image(images2[(frame + 1 + wheel2) % numFrames], 600, 200); //this is the image to test
- image(images2[(frame + 2 + wheel2) % numFrames], 600, 400);
- //slow stuff down so we can see it
- //frame = (frame+1) % numFrames;
- delay(200);
- //bg
- // print(frame);
- }
- void winner() {
- //are the names of the images the same
- if (a == b && b == c ) {
- // display a question from you list of questions by generating a random number and selecting the text
- q = (int) random(3);
- //display question, question size
- text(questions[q],20,20);
- //calculate how much time has passed
- int passedTime = second() - savedTime;
- //has five seconds passed?
- if (passedTime > totalTime) {
- print("times up" );
- }
- }
- }
- //text
1