gpt4 book ai didi

java - Slick addState java.lang.Error

转载 作者:行者123 更新时间:2023-12-01 12:19:13 26 4
gpt4 key购买 nike

好吧,所以我做错了什么,但我不知道是什么。我正在学习有关使用 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();
}
}
}

这是类(class)

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)

最佳答案

由于 Game 扩展了 StateBasedGame 并且它不会覆盖 addState(),当您说 this.addState(new Menu(menu)); 它正在尝试调用 StateBasedGame API referance 中定义的方法

所以你的 MenuPlay 类应该是 GameState 的子类

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com