Reputation: 49
I'm trying to write a function into my program that suspends draw() for 3 seconds when the mouse is clicked. When the timer passes 3 seconds, draw() should resume, however it's not.
Deck playerDeck;
Deck computerDeck;
PFont font;
int timer; //use this to allow cards to stay drawn for 2 or 3 seconds
int cardsLeft = 52;
**int savedTime;
int totalTime = 3000;** //milliseconds
void setup(){
size(500,500);
frameRate(30);
playerDeck = new Deck(true,215,364,71,96);
computerDeck = new Deck(false,215,40,71,96);
font = loadFont("TimesNewRomanPSMT-20.vlw");
textFont(font, 20);
textAlign(CENTER);
**savedTime = millis();**
}
void draw(){
background(255);
//draws the players' decks
playerDeck.drawDeck();
computerDeck.drawDeck();
//informative text
fill(0);
text("Player",width/2,493);
text("Computer",width/2,27);
text(cardsLeft + " Cards left",width/2,height/2+5);
}
void mousePressed(){
if(cardsLeft > 0){ //checks cards left aka clicks, limited to 52- size of deck
if(playerDeck.deckClicked(mouseX,mouseY)){//checks if player deck is clicked
println("You picked a card from your deck");
playerDeck.drawCardAndCompare();//draws a random card for the player from a 2d array suit->card
computerDeck.drawCardAndCompare();//draws a random card for the computer from a 2d array suit->card.
cardsLeft--;
} else if(computerDeck.deckClicked(mouseX,mouseY)){//checks if the player clicked the computers deck. no need for computer interactivity so computer and player draws are simultaneous
println("You can't take cards from the computer's deck!");
} else {
println("Click on your deck to pick a card");//if the player clicks elsewhere
}
} else {
println("Game over"); //when cards left / clicks equals or is less then 0
}
**noLoop();**
}
**void mouseReleased(){
int passedTime = millis() - savedTime;
if(passedTime > totalTime){
loop();
savedTime = millis();
}
}**
Upon pressing the mouse, a couple of images draw on the screen. Upon releasing the mouse, a timer is supposed to set for 3 seconds and after three seconds have passed it activates the loop() to draw over the images. The issue here is that holding the mouse button for 3 seconds than releasing will activate loop() OR clicking again if you released the mouse button before three seconds. I'm sorry if I'm not clear, I'm really tired at the moment but need to finish this work.
Upvotes: 0
Views: 113
Reputation: 4736
You could just get the main thread to sleep for 3 seconds when the mouse is released. it should then resume after the time has elapsed. Change your event handler to this:
@Override
public void mouseReleased(MouseEvent e) {
try {
Thread.sleep(3000);
//thread will then resume
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
Upvotes: 1