【问题标题】:slick2D spritesheet doesn't loadslick2D spritesheet 不加载
【发布时间】:2014-10-17 18:52:59
【问题描述】:

我有一个问题,我对 slick 很陌生,我正在制作一个横向卷轴。我正在尝试在 stateBasedGame 中加载精灵表。但是当我运行/调试游戏时,它运行但不显示图像。代码如下:

//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);

我仔细检查了路径,这是正确的。请帮我!

【问题讨论】:

    标签: image 2d sprite tile slick2d


    【解决方案1】:

    没错,问题是你没有调用 draw 方法,你只是获取了图像的一个实例。

    试试这个: Resources.getSpriteImage("tiles", 0, 0).draw(0, 0);

    【讨论】:

    • 好吧,我使用了那个代码,但现在我有这个 NullPointerException:
    【解决方案2】:

    你的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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 2013-06-23
      • 2011-07-14
      • 1970-01-01
      • 2015-08-08
      • 2015-11-16
      • 1970-01-01
      相关资源
      最近更新 更多