How to make a timer while keyPressed

edited October 2017 in Questions about Code

hello,

Im new to processing and I cant figure out how to begin my program with a startpage, then when a key is pressed show a different page with a countdown of 10 seconds and a red blinking circle. After 10 seconds you release the key and it shows a message and closes the program.

I got a few things done like it begins at the startpage but the timer already starts running when i run the program, not when the key is pressed. It should begin counting when the key is pressed When i hold a key it does jump to the "holding a key" page. i tried a lot of other methods with a timer but they all failed because the timer doenst start when the key is pressed.

If someone could give me some tips it would be great!

code:

int fadeAmount = 5;
int opacity = 255;
int circleSize = 75;
int circleYpos = 700;
int circleXpos = 700;
int timer;
int count = 10;
int trigger = 0;


void setup() {
  size(800, 800); 
  smooth();
  background(250);
} 

void draw() {

  if (keyPressed == true) {
    circle_on();
  } else if (keyPressed == true || timer > 10000) {
    processing();
  } else {
    startPage();
  }
}

void startPage() {
  fill(0);
  background(250);
  textSize(50);
  text("Start", width/2 - 50, height/2);
}

void circle_on() {

  background(250);
  fill(255, 2, 2, opacity);
  noStroke();
  ellipse(circleXpos, circleYpos, circleSize, circleSize);
  if ((opacity ==0) || (opacity ==255))  
    fadeAmount = -fadeAmount;

  opacity += fadeAmount;

  fill(0);
  textSize(50);
  text("Are you sure?", 200, 720);
}



void processing() {
  background(250);
  fill(0);
  textSize(50);
  text("Agreement success.", 150, 720);
  fill(250);
  noStroke();
  ellipse(circleXpos, circleYpos, circleSize, circleSize);
}

void keyPressed() {
  timer = millis();

}
Tagged:

Answers

  • Answer ✓

    See changes below.

    Kf

    int fadeAmount = 5;
    int opacity = 255;
    int circleSize = 75;
    int circleYpos = 700;
    int circleXpos = 700;
    int timer=0;
    int count = 10;
    int trigger = 0;
    
    boolean activateCircle=false;
    
    
    void setup() {
      size(800, 800); 
      smooth();
      background(250);
    } 
    
    void draw() {
    
      if (activateCircle == true) {
        circle_on();
        if ( (millis()-timer) > 10000) {
          processing();
        }
      } else {
        startPage();
      }
    }
    
    void keyPressed() {
    
      activateCircle=true;
      timer = millis();
    }
    
    
    void startPage() {
      fill(0);
      background(250);
      textSize(50);
      text("Start", width/2 - 50, height/2);
    }
    
    void circle_on() {
    
      background(250);
      fill(255, 2, 2, opacity);
      noStroke();
      ellipse(circleXpos, circleYpos, circleSize, circleSize);
      if ((opacity <0) || (opacity >255))  
        opacity = 255;
    
      opacity -= fadeAmount;
    
      fill(0);
      textSize(50);
      text("Are you sure?", 200, 720);
    }
    
    
    
    void processing() {
      background(250);
      fill(0);
      textSize(50);
      text("Agreement success.", 150, 720);
      fill(250);
      noStroke();
      ellipse(circleXpos, circleYpos, circleSize, circleSize);
    }
    
Sign In or Register to comment.