-2

I have just make an object move continuously and Now I want to make it stop in 10 seconds if the space bar is pressed?

Does anyone know how to implement this in processing? Thank you in advanced!

George Profenza
  • 50,687
  • 19
  • 144
  • 218
ntony2000
  • 1
  • 1
  • Please post your code so far – g00se Oct 13 '22 at 10:16
  • In Java (since the question is marked with the [tag:java] tag) we can use a `SwingWorker`, some `KeyListener` and `javax.swing.Timer` – user16320675 Oct 13 '22 at 10:25
  • @ntony2000 you can use a boolean variable to keep track of whether space was pressed or not. You can then use `millis()` and two variables to check current time vs elapsed time to know when 10 seconds bassed to stop the object movement. You can find a [millis() example here](https://stackoverflow.com/questions/12417937/create-a-simple-countdown-in-processing/12421641#12421641) – George Profenza Oct 13 '22 at 12:47

1 Answers1

2

By default, Processing draws at 60 frames/second, so you can use that to your advantage.

//Global Variables
int countDownTimer = 600 //10 seconds * 60 FPS
boolean spacePressed = false;

void draw(){
  if(spacePressed){
    countDownTimer--;
    if(countDownTimer == 0){
      //Whatever happens after 10 seconds here
    }
  }
}

void keyPressed(){
   if(key == " "){
     spacePressed = true;
   }
}