我正在尝试在我的程序中编写一个函数,在单击鼠标时暂停draw()3秒钟。当计时器超过3秒时,draw()应该恢复,但不是。
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();
}
}**
按下鼠标后,屏幕上会显示几张图像。释放鼠标时,计时器应设置为3秒,经过三秒后,它会激活循环()以绘制图像。这里的问题是按住鼠标按钮3秒钟而不是释放将激活循环()或如果在3秒之前释放鼠标按钮再次单击。如果我不清楚,我很抱歉,我现在真的很累,但需要完成这项工作。
答案 0 :(得分:1)
释放鼠标时,你可以让主线程休眠3秒钟。它应该在经过一段时间后恢复。将您的事件处理程序更改为:
@Override
public void mouseReleased(MouseEvent e) {
try {
Thread.sleep(3000);
//thread will then resume
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}