[英]Swing timer not working
我正在為一個班級創建一個街機游戲。 游戲是專欄。 我正在嘗試使所有自由浮動塊每秒下降一次。 我的使它們掉落的代碼有效,但是計時器不起作用。 這是我代碼的相關部分。
前兩個出現在我用來管理內部邏輯的BlockManager對象中
public void play(){
t = new Timer(1000, this);
t.start();
}
public void actionPerformed(ActionEvent e) {
for(int v = 0;v < this.x; v++){
if(v > this.x || v < 0){
break;
}
for(int b = 0;b < this.y ;b++){
this.gameScreen.get(v).get(b).setActive(true);
}
}
for(int i = 0;i < this.x; i++){
if(i > this.x || i < 0){
break;
}
for(int j = 0;j < this.y ;j++){
if(this.gameScreen.get(i).get(j).getActive() == true){
if(j + 1 < this.y){
if(this.gameScreen.get(i).get(j+1).toString().equals(".")){
Collections.swap(this.gameScreen.get(i), j, j+1);
this.gameScreen.get(i).get(j+1).setActive(false);
}
}
}
}
}
}
this is the main method that is another file.
public static void main(String[] args){
ColumnsUI Col = new ColumnsUI(9,9);
Col.game.nextBlock();
Col.gw.add(Col.show);
Col.game.play();
}
列UI對象是一種基於內部邏輯進行繪制的簡單方法。 我懷疑這與計時器故障有關,因為它在我手動調用actionPerformed的內容時起作用。
謝謝你的幫助!
當某些事件發生時會調用actionPerfomed。 您應該為計時器創建一個單獨的類。 就像在此代碼中一樣,Task類當然必須由您的代碼替換,這會移動模塊。
Timer timer = new Timer();
// Start in 1 second and then all 5 seconds forever repatedly
timer.schedule( new Task(), 1000, 5000 );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.