Java Eclipse线程错误
我开始钻研minecraft迷你游戏插件,最近我开发了一个团队死亡竞赛游戏插件,它需要一个线程来保持倒计时时钟运行。计时器线程的代码保存在GameTimer类中,该类由要在Game类中启动的StartName方法调用。 Eclipse一直说“启动方法对于GameTimer类型是未定义的”,这很奇怪,因为GameTimer类实现了Runnable接口并在Game类中实例化 GameTimer方法如下所示:Java Eclipse线程错误,java,multithreading,minecraft,Java,Multithreading,Minecraft,我开始钻研minecraft迷你游戏插件,最近我开发了一个团队死亡竞赛游戏插件,它需要一个线程来保持倒计时时钟运行。计时器线程的代码保存在GameTimer类中,该类由要在Game类中启动的StartName方法调用。 Eclipse一直说“启动方法对于GameTimer类型是未定义的”,这很奇怪,因为GameTimer类实现了Runnable接口并在Game类中实例化 GameTimer方法如下所示: package game.start.time; public class GameTim
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;
}
}
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();
}
}
编辑***以下是错误的图片:
将
游戏计时器
封装在线程中
以便可以调用
new Thread(new GameTimer()).start();
阅读:请不要链接到错误图片。相反,请将您的实际错误和完整错误文本与您的问题一起发布在此处。您不能互换地使用
run
和start
(查看方法!)这不是Eclipse错误,事实上,我们的问题与Eclipse完全无关(标记已删除)。这是一条Java编译器错误消息,警告您试图调用一个不存在的方法。Runnable是一个接口,它有一个方法,run()
,就是这样,实现Runnable的类必须实际实现该方法。你可以考虑学习一本关于java的入门书,因为这对你非常有用。非常感谢你,解决了一切!