gpt4 book ai didi

java - 我无法用 slick2D Java 解决这个问题

转载 作者:行者123 更新时间:2023-11-29 05:44:45 24 4
gpt4 key购买 nike

我正在尝试为游戏制作启动画面。我搜索谷歌无济于事。我正在学习本教程 here这对他有用。我的代码有点不同,但看起来有点像他的。这是我的代码:

游戏类:

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Game extends StateBasedGame {

public static final String gamename = "Life - Alpha";
public static int menu = 1;
public static int play = 2;
public static int splash = 0;

public Game(String gamename) {
super(gamename);
}

public void initStatesList(GameContainer gc) throws SlickException {
this.getState(menu).init(gc, this);
this.getState(play).init(gc, this);
this.enterState(0);
}

public static void main(String[] args) {

AppGameContainer app;
try {
app = new AppGameContainer(new Game(gamename));
app.setDisplayMode(800, 600, false);
app.start();
} catch (SlickException e) {
e.printStackTrace();
}

}
}

菜单类别:

import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Menu extends BasicGameState {

public Menu(int state) {

}

public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {

}

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {

Image back = new Image("res/Back.png");
g.drawImage(back, 0, 0);

Image panel = new Image("res/Panel.png");
g.drawImage(panel, 240, 100);

Image logo = new Image("res/Life Alpha.png");
g.drawImage(logo, 290, 130);

Image playButton = new Image("res/Play.png");
g.drawImage(playButton, 330, 310);

Image exitButton = new Image("res/Exit.png");
g.drawImage(exitButton, 330, 370);

}

public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {

Input input = gc.getInput();
int xpos = Mouse.getX();
int ypos = Mouse.getY();

if((xpos > 330 && xpos < 480) && (ypos > 250 && ypos < 290)) {
if(input.isMousePressed(0)) {
sbg.enterState(2);
}
}
}

public int getID() {
return 0;
}

}

玩类:

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Play extends BasicGameState {

public Play(int state) {

}

public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {

}

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {

Image back = new Image("res/Back.png");
g.drawImage(back, 0, 0);

Image panel = new Image("res/Panel2.png");
g.drawImage(panel, 170, 100);

}

public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {

}

public int getID() {
return 1;
}

}

启动画面类:

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class SplashScreen extends BasicGameState {

Image splash;

private int elapsedTime;
private final int DELAY = 3000;

public SplashScreen(int state) {

}

public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {

splash = new Image("res/SplashScreen.png");

}

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {

g.drawImage(splash, 0, 0);

}

public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {

elapsedTime += delta;

if(elapsedTime >= DELAY) {

sbg.enterState(1);

}

}

public int getID() {
return 0;
}

}

我得到的错误是:

2013 年美国东部时间 4 月 27 日星期六 22:48:01 信息:Slick Build #274

2013 年美国东部时间 4 月 27 日星期六 22:48:01 信息:LWJGL 版本:2.8.5

2013 年美国东部时间 4 月 27 日星期六 22:48:01 信息:原始显示模式:1366 x 768 x 32 @60Hz

2013 年美国东部时间 4 月 27 日星期六 22:48:01 信息:TargetDisplayMode:800 x 600 x 0 @0Hz

2013 年美国东部时间 4 月 27 日星期六 22:48:01 信息:开始显示 800x600

2013 年美国东部时间 4 月 27 日星期六 22:48:01 信息:使用 Java PNG Loader = true

2013 年美国东部时间 4 月 27 日星期六 22:48:01 信息: Controller 不可用

线程“main”中的异常 java.lang.NullPointerException

在 mms001.Game.initStatesList(Game.java:18)

在 org.newdawn.slick.state.StateBasedGame.init(StateBasedGame.java:164)

在 org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:390)

在 org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:314)

在 mms001.Game.main(Game.java:29)

最佳答案

看起来这里可能有几个问题。

我注意到您的 Menu 类返回 0 作为它的 ID,SplashScreen 类也是如此。我敢打赌,如果您将 Menu 类的 getID 更改为 1,那将解决您的部分问题。

class Menu...
public int getID() {
return 1;
}

您还需要更新 Play 类的 getID 方法以返回 2。

class Play...
public int getID() {
return 2;
}

一旦你这样做,它们将匹配你在主类中定义的静态字段。

然后更新您的initStatesList 方法以初始化splash 并进入菜单 状态

public void initStatesList(GameContainer gc) throws SlickException {
this.getState(splash).init(gc, this);
this.getState(menu).init(gc, this);
this.getState(play).init(gc, this);

this.enterState(menu);
}

希望这对您有所帮助。

关于java - 我无法用 slick2D Java 解决这个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16252319/

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