我需要一个计时器用于我正在构建的游戏,它基本上做的是启动一个事件,当我点击游戏时每秒移动并对象一个正方形.现在要么我让游戏顺其自然(游戏结束或对象移出界限)或再次按下游戏,计时器似乎会触发对象比以前更快地移动等等,每次都越来越快我重新启动时间.
private void playButtonMouseClicked(java.awt.event.MouseEvent evt) {
/*code*/
timer = new Timer(timerSpeed, new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
/*code that ends with something that calls timer.stop()*/
}
}
});
if(timer.isRunning()) //in case I don't let the game stop the timer
timer.stop();
timer.start();
}
Run Code Online (Sandbox Code Playgroud)
我检查过timer.getDelay(),延迟没有改变,它保持不变,但我可以看到每次箭头移动越来越快.我正在使用jPanels和带有和图标的标签来显示网格和移动对象.
有任何想法吗?