好的,所以我做错了什么,我无法弄清楚是什么。我正在学习关于使用Slick构建简单游戏的教程。我知道代码中几乎没有任何东西,但此时代码应该能够编译。
package javagame;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Game extends StateBasedGame{
public static final String gamename = "Game name!";
public static final int menu = 0;
public static final int play = 1;
public Game(String gamename){
super(gamename);
this.addState(new Menu(menu));
this.addState(new Play(play));
}
public void initStatesList(GameContainer gc) throws SlickException{
this.getState(menu).init(gc, this);
this.getState(play).init(gc, this);
this.enterState(menu);
}
public static void main(String[] args) {
AppGameContainer appgc;
try{
appgc = new AppGameContainer(new Game(gamename));
appgc.setDisplayMode(640, 360, false);
appgc.start();
}catch(SlickException e){
e.printStackTrace();
}
}
}
以下是课程
package javagame;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Play {
public Play(int state){
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
}
public int getID(){
return 1;
}
}
和
package javagame;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Menu {
public Menu(int state){
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
}
public int getID(){
return 0;
}
}
错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method addState(GameState) in the type StateBasedGame is not applicable for the arguments (Menu)
The method addState(GameState) in the type StateBasedGame is not applicable for the arguments (Play)
at javagame.Game.<init>(Game.java:12)
at javagame.Game.main(Game.java:25)
答案 0 :(得分:1)
由于游戏延伸StateBasedGame
并且未覆盖addState()
,当您说this.addState(new Menu(menu));
时,它会尝试调用StateBasedGame
API referance中定义的方法
因此,您的Menu
和Play
类应该是GameState