我有个问题,我是个新手,我正在做一个侧滚动游戏。我试图在一个基于状态的游戏中加载一个spriteheet。但当我运行调试游戏时,它运行了,但不显示图像。下面是代码。
//private static Map<String, Image> images;
private static Map<String, SpriteSheet> sprites;
//private static Map<String, Sound> sounds;
public Resources() {
//images = new HashMap<String, Image>();
sprites = new HashMap<String, SpriteSheet>();
//sounds = new HashMap<String, Sound>();
try {
sprites.put("tiles", loadSprite("resources/tiles.png", 32, 32));
} catch (SlickException e) {
e.printStackTrace();
}
}
private Image loadImage(String path) throws SlickException {
return new Image(path, false, Image.FILTER_NEAREST);
}
private SpriteSheet loadSprite(String path, int tw, int th) throws SlickException {
return new SpriteSheet(loadImage(path), tw, th);
}
public static Image getSpriteImage(String getter, int x, int y) {
sprites.get(getter).getSubImage(x, y);
return null;
}
我在GameState里放了这个
Resources.getSpriteImage("tiles", 0, 0);
我仔细检查了路径,这是正确的。请帮助我
这是正确的,问题是你没有调用绘制方法,你只得到了一个图像的实例。
试试这个:Resources.getSpriteImage("tiles", 0, 0).draw(0, 0)。
你的方法getSpriteImage需要返回图像,而不是返回null。
public static Image getSpriteImage(String getter, int x, int y) {
return sprites.get(getter).getSubImage(x, y);
}
不要忘了在你的GameState中实际绘制磁砖
Resources.getSpriteImage("tiles", 0, 0).draw(0, 0);