在运行代码以显示 pacman 级别背景时,我遇到了图像和 pacman sprite 未加载的问题。
例外:
Exception in thread "Thread-2" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1388)
at org.pacman.Pacman.init(Pacman.java:51)
at org.game.engine.GameLoop.run(GameLoop.java:26)
GameLoop 的第 26 行很简单game.init();
。
这是课程:
public class Pacman extends Game {
public static void main(String[] args) {
GameApplication.start(new Pacman());
}
private BufferedImage sheet;
private int frame;
public Pacman() {
title = "Pacman";
width = 176;
height = 258;
frame = 0;
}
@Override
public void draw(Graphics g) {
if (sheet != null) {
g.drawImage(sheet, -2, 0, null);
//draw ms pacman
g.drawImage(sheet.getSubimage(628+(frame/3)*(15+2), 33, 15, 15), 80, 105, null);
}
}
@Override
public void init() {
try {
sheet = ImageIO.read(Pacman.class.getResource("/images/pacman.png"));
} catch (IOException ex) {
Logger.getLogger(Pacman.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void update() {
frame++;
if (frame > 3*2) {
frame = 0;
}
}
}