【问题标题】:Libgdx fail to draw spriteLibgdx 无法绘制精灵
【发布时间】:2014-02-15 08:35:02
【问题描述】:

我非常努力地用 LibGDX 绘制精灵,但我失败了。如果您需要的不仅仅是 sn-ps,来源在这里:https://github.com/reisi007/Planes2DLibGDX/blob/master/Main/src/com/reisisoft/Planes2D/Planes2D.java

那么,我在做什么? 我正在加载 PNG (1024x1024) 并创建精灵,并将其放入地图中。这张地图我又放了一张地图(后来不同分辨率)

 private void putTextures(Resolutions resolutions) {
    Map<GObjects, Sprite> map;
    switch (resolutions) {
        case MidRes:
            map = new EnumMap<GObjects, Sprite>(GObjects.class);
            map.put(GObjects.PlaneR, new Sprite(txtMidRes, 0, 0, 600, 278));
            map.put(GObjects.PlaneG, new Sprite(txtMidRes, 0, 278, 600, 278));
            map.put(GObjects.PlaneB, new Sprite(txtMidRes, 0, 556, 600, 104));
            map.put(GObjects.Grass, new Sprite(txtMidRes, 0, 834, 600, 104));
            map.put(GObjects.Bullet, new Sprite(txtMidRes, 600, 834, 299, 138));
            all.put(Resolutions.MidRes, map);
            break;
    }
}

有一个函数我可以从当前分辨率中得到一个精灵:

private boolean setCurrentResolutions(Resolutions resolution) {
    if (resolution == null)
        return false;
    currentResolution = resolution;
    currentResolutionSprites = all.get(Resolutions.MidRes);
    return true;
}

private Sprite requestSprite(GObjects gObject) {
    return currentResolutionSprites.get(gObject);
}

我正在测试大小为 800x480 的 Winows 版本。 我要绘制的精灵的原点是:X 300.0 Y 139.0 宽度为 300,高度为 140。 接下来是渲染函数:

public void render() {
    camera.update();
    spriteBatch.setProjectionMatrix(camera.combined);
    Update();
    spriteBatch.begin();
    Draw();
    spriteBatch.end();
}

由于这并没有真正的帮助,这里是 Draw 方法:

public void Draw() {
    s.draw(spriteBatch);
}

我看到的只是黑色。 我尝试使用 OpenGL 1、1.1、2 和 StackOverflow 的各种解决方案

最后是“create”方法:

// On creation
public void create() {
    curH = Gdx.graphics.getHeight();
    curW = Gdx.graphics.getWidth();
    camera = new OrthographicCamera(curW, curH);
    spriteBatch = new SpriteBatch();
    // Load texture
    txtMidRes = new Texture(Gdx.files.internal("planes.png"));
    txtMidRes.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
    // Create Sprites for EVERY supported resolution
    putTextures(Resolutions.MidRes);
    // Set resolution according to H/W
    setCurrentResolutions(Resolutions.MidRes);
    s = requestSprite(GObjects.PlaneR);
    s.setSize(300, 140);
    s.setPosition(Gdx.graphics.getWidth() / 2 - s.getWidth() / 2,
            Gdx.graphics.getHeight() / 2 - s.getHeight() / 2);
}

感谢您的帮助

弗洛里安

【问题讨论】:

    标签: java libgdx


    【解决方案1】:

    首先,您提供的完整源代码链接与那里的 sn-ps 不匹配。也许有一个被遗忘的推动?

    现在关于您的问题的一些想法:

    • 您尚未设置相机位置。试试看:

      camera.position.set(curH / 2f, curW / 2f, 0);

    • 您不需要手动构建精灵,libgdx 提供 功能来做到这一点。请参阅官方文档上的Texture packer

    • 您还可以使用AssetManager 自动处理分辨率。 请参阅this post 了解更多信息。

    【讨论】:

    • 谢谢,更新相机位置确实有帮助:)
    猜你喜欢
    • 2023-03-31
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-27
    • 2016-03-13
    • 1970-01-01
    相关资源
    最近更新 更多