Java计时器加速每次执行

时间:2013-05-15 14:49:21

标签: java timer

每次运行计时器时,计时器内执行的代码运行得更快。为什么会这样?

此代码实际上是在框架上移动标签(保持图像)。 每次运行,图像移动得更快。

enter image description here

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速度。然后在下一次运行时,它执行得更快,更快,更快。

2 个答案:

答案 0 :(得分:2)

我认为你应该只添加一次动作监听器。

答案 1 :(得分:0)

只需添加一次Ther actionlistener。

此外,如果这不能解决问题,那么你未发布的代码中的某个位置会应用速度,这可能不会被重置并添加到其上,这会使速度提高一倍。

相关问题