Java 当我开始一个新游戏时,游戏中的一切都开始以双倍的速度进行。Can';不要停止计时器

Java 当我开始一个新游戏时,游戏中的一切都开始以双倍的速度进行。Can';不要停止计时器,java,timer,delay,Java,Timer,Delay,我正在做一个蛇克隆,在我开始新游戏之前一切都很顺利。然后一切都工作了,但一切都是双倍速度!我认为这是因为有两个计时器同时工作,执行相同的功能?我找不到办法来处理这件事。这就是我的计时器的样子,正如你所看到的,它相当混乱:/甚至不要试图弄清楚这一切。我的蛇由球组成,大部分代码跟踪它们的运动 public class Draw extends JPanel implements KeyListener{ static ArrayList x = new ArrayList(); //the x c

我正在做一个蛇克隆,在我开始新游戏之前一切都很顺利。然后一切都工作了,但一切都是双倍速度!我认为这是因为有两个计时器同时工作,执行相同的功能?我找不到办法来处理这件事。这就是我的计时器的样子,正如你所看到的,它相当混乱:/甚至不要试图弄清楚这一切。我的蛇由球组成,大部分代码跟踪它们的运动

public class Draw extends JPanel implements KeyListener{

static ArrayList x = new ArrayList(); //the x coordinates of balls
static ArrayList y = new ArrayList(); //the y coordinates of balls
static int x_val=15; // movement x-axis
static int y_val=0; // movement y-axis
static boolean ballCoordinatesDone = false; //initialize balls
static int score = 0; 
static boolean snakeCrashed = false; //if snake has crashed (stop)
static boolean highscoresOpened = false; //if highscores are opened already (don't repeat)

static int foodx = 0; //food x coordinate
static int foody = 0; //food y coordinate


Timer  t = new Timer(150, new ActionListener(){
    public void actionPerformed(ActionEvent e){
        if(snakeCrashed == false){
        if(ballCoordinatesDone == false){
        ballCoordinates();
        }
        ballCoordinatesDone = true;

        int lastX = Integer.parseInt((String) x.get(x.size()-1)); //gets x coodinate for first ball
        int lastY = Integer.parseInt((String) y.get(y.size()-1)); //gets y coodinate for first ball
        int a;

//checks if snake collides with itself
        for(a=0; a < x.size(); a++){

            if(lastX + x_val == Integer.parseInt((String) x.get(a)) && lastY + y_val == Integer.parseInt((String) y.get(a))){

                snakeCrashed = true;

            }
        }

//checks if snake goes out of bounds
        if(lastX + x_val < 5  || lastY + y_val < 50 || lastX + x_val >= 310  || lastY + y_val >= 355  ){

            snakeCrashed = true;

        }else{

//adds a new ball in direction of movement (new head)
        x.add("" + (lastX + x_val));
        y.add("" + (lastY + y_val));

        }

//checks if you hit a food
        if(lastX + x_val == foodx && lastY + y_val == foody){

            foodRandomizer();
            score+=10;

        }else{
 //removes the tail     
            if(snakeCrashed == false){
            x.remove(0);
            y.remove(0);
            }

        }

        repaint();

//if snakeCrashed == true   
        }else{
            if(highscoresOpened == false){ 
            CreateGUI.highscoreFrame(); 
            highscoresOpened = true;
            t.stop();


            }
        }
    }

});

每个Draw对象都会创建一个新的计时器,使游戏同时运行n次,但游戏副本共享该状态,因为ir存储在静态变量中,因此状态更新速度比应该的快n-1倍


您可以停止计时器,也可以使用静态计时器,每次都可以。

使用此功能,因为您还没有发布详细信息,因此帮助您会有点困难。但是什么时候创建第二个计时器呢?为什么不创建第二个计时器?@user270349这更像是一个修辞问题,目的是让OP考虑调试自己的程序。类型timer的方法cancel()未定义。我用的是摆动计时器。没有这样的命令。凯文,谢谢你的帮助,但我知道这个问题,但我是一个初学者,不知道如何修复或调试它。我已经试了好几个小时了。由于某种原因,它开始与“t.removeActionListener()”一起工作,为什么不与t.stop()一起工作;?
JButton newGame = new JButton("New Game");
    newGame.setBounds(75,300,100,40);
    newGame.setVisible(true);
    newGame.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            hsFrame.setVisible(false);
            hsFrame.dispose();
            submitHSFrame.setVisible(false);
            submitHSFrame.dispose();
            snake.setVisible(false);
            snake.dispose();
            Draw.snakeCrashed = false;
            Draw.highscoresOpened = false;
            Draw.x.clear();
            Draw.y.clear();
            Draw.score = 0;
            Draw.x_val=15;
            Draw.y_val=0;
            Draw.ballCoordinatesDone = false;
            Draw.score = 0;

            CreateGUI ng = new CreateGUI();

        }
    }); 

    hsFrame.add(newGame);