[英]Game timer won't stop
我試圖設置一個計時器來隨機設置屏幕中的幀,但是然后根據boolean
,將其設置回正常狀態。 我已經嘗試過.cancel()
但是不起作用。 我如何才能停止此計時器?:
if(e.getSource()==impossible){
impossiblity=!impossiblity;
System.out.println(impossiblity);
if(!impossiblity){
new Timer().schedule(new TimerTask() {
public void run() {
int randWidth=(int)((dim.width-gameFrame.getSize().width) * Math.random());
int randHeight=(int)((dim.height-gameFrame.getSize().height) * Math.random());
gameFrame.setLocation(randWidth, randHeight);//sets location to center
}
}, 1000, 1000);
} else gameFrame.setLocation(dim.width/2-gameFrame.getSize().width/2, 0);//sets Location to center of screen
}
現在它確實將游戲框架設置為正常,但隨后又回到隨機
通常,嘗試從線程外部取消計時器或其他線程是一個壞主意,並且會導致各種問題。
最好的選擇是設置某種標志或計數器,以便TimerTask在運行時進行檢查。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.