[英]Java Timer Speeds Up Every Execution
每次運行計時器時,計時器內執行的代碼運行得更快。 為什么會這樣?
此代碼基本上在框架上移動標簽(保持圖像)。 每次運行,圖像移動得更快。
if (Player.direction == "west") {
timerWest.start();
isCasting = true;
new Magic("westmagic.gif");
timerWest.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int x = Frame.fireMagicLabel.getX();
int y = Frame.fireMagicLabel.getY();
Frame.fireMagicLabel.setLocation(x - 1, y);
LiveUpdating.updateSpell();
CheckHits.spellHit();
Frame.frame.repaint();
if (Frame.fireMagicLabel.getX() <= tempWest) {
timerWest.stop();
new Magic("");
Frame.frame.repaint();
Frame.fireMagicLabel.setLocation(
Frame.redHealthLabel.getX(),
Frame.redHealthLabel.getX());
isCasting = false;
}
}
});
Frame.frame.repaint();
}
首次運行時,計時器執行x速度。 然后在下一次運行時,它執行得更快,更快,更快。
我認為你應該只添加一次動作監聽器。
Ther actionlistener只需添加一次。
此外,如果這沒有解決它,你沒有發布的代碼內部的某個地方應用速度,這可能不會被重置並添加到它上,這使它加倍速度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.