Eclipse线程错误

时间:2014-12-13 02:46:07

标签: java multithreading minecraft

我开始深入研究Minecraft迷你游戏插件,我最近开始了一个团队死亡竞赛游戏插件,这需要一个线程来保持倒计时时钟的运行。计时器线程的代码保存在GameTimer类中,该类由startGame方法调用,以在Game类中启动。 Eclipse一直说"对于GameTimer"类型,start方法是未定义的,考虑到GameTimer类实现Runnable接口并在Game类中实例化,这很奇怪

GameTimer方法如下所示:

package game.start.time;

public class GameTimer implements Runnable {

    //Initial arguments for setting length of game in minutes and seconds
    //Stored as reference and are not modified
    int gameMinutes;
    int gameSeconds;

    //values for minutes and seconds to be used by the time thread
    int minutes;
    int seconds;

    // value to denote if time is up for te game or not
    boolean timeDone;

    //constructor
    public GameTimer(int minutesIn){

        gameMinutes = minutesIn;

        gameSeconds = 0;

        minutes = minutesIn;

        seconds = 0;

    }


    //returns a formatted string that prints the time in clock format
    public String printTime(int minutes, int seconds){

        if(seconds<10){

            return String.format("%d:0%d",minutes,seconds);
        }else{

            return String.format("%d:%d",minutes,seconds);

        }

    }


    //thread which counts down time
    @Override
    public void run() {


        try {
            String toPrint = printTime(minutes,seconds);

            seconds--;

            if(seconds==0){

                seconds = 59;
                minutes--;
            }

                if(minutes == 0 && seconds == 0){

                    setTimeUp(true);

                }
        } catch (Exception e) {

            e.printStackTrace();
        }

        }


        //sets timeIsUp value
        public void setTimeUp(boolean timeIsUp){

            timeDone = timeIsUp;



    }

    //passes timeIsUp value to calling methods
    public boolean getTimeDone(){


        return true;
    }



    }

StartGame方法如下所示:

package game;

import game.start.time.GameTimer;



public class Game {

    public boolean teamsRegistered;
    public boolean spawnPointsRegistered;
    public boolean gameTimeSet;
    int gameTime;

    GameTimer timer = new GameTimer(gameTime);

    public Game(int gameTimeIn){

        gameTimeSet = true;
        gameTime = gameTimeIn;

    }

    public void startGame(){

        timer.start();

    }



}

编辑***以下是错误图片:

http://prntscr.com/5g7zdw

1 个答案:

答案 0 :(得分:3)

GameTimer括起来Thread,以便调用start

new Thread(new GameTimer()).start();

阅读:Defining and Starting a Thread