好的,所以我做错了事,我不知道该怎么办。我正在学习有关使用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)

最佳答案

随着游戏扩展StateBasedGame并且不会覆盖addState(),当您说this.addState(new Menu(menu));时,它将尝试调用StateBasedGame API referance中定义的方法

因此,您的MenuPlay类应该是GameState的子类

关于java - 光滑的addState java.lang.Error,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26809593/

10-10 03:41