简体   繁体   English

libgdx无法绘制精灵

[英]Libgdx fail to draw sprite

I am trying really hard to draw a sprite with LibGDX, but I fail. 我非常努力地用LibGDX绘制精灵,但是失败了。 If you need more than the snippets, the source is here: https://github.com/reisi007/Planes2DLibGDX/blob/master/Main/src/com/reisisoft/Planes2D/Planes2D.java 如果您需要的不仅仅是片段,请在此处查找源: https : //github.com/reisi007/Planes2DLibGDX/blob/master/Main/src/com/reisisoft/Planes2D/Planes2D.java

So, what am I doing? 那么,我在做什么? I am loading a PNG (1024x1024) and creating sprites, which I put in a Map. 我正在加载PNG(1024x1024)并创建精灵,并将其放置在地图中。 This map I put in a map again (later different resolutions) 我再次放入地图(分辨率不同)

 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;
    }
}

There is a function I get a sprite from the current resolutions: 我可以从当前的分辨率中得到一个精灵:

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

I am testing the Winows build, whichs size is 800x480. 我正在测试Winows版本,其尺寸为800x480。 The origin of the sprite I want to draw is: X 300.0 Y 139.0 The width is 300, height is 140. Next the render function: 我要绘制的精灵的原点是:X 300.0 Y 139.0宽度为300,高度为140。接下来是render函数:

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

As this is not really helping, here the Draw method: 由于这实际上并没有帮助,因此这里使用Draw方法:

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

What I see is just black. 我看到的只是黑色。 I tried with OpenGL 1,1.1,2 and various solution from StackOverflow 我尝试使用OpenGL 1,1.1,2和StackOverflow的各种解决方案

Finally the "create" method: 最后是“创建”方法:

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

Thanks for helping 感谢您的帮助

Florian 弗洛里安

First of all, the link you provided to the full source doesn't match the snippets there. 首先,您提供给完整源的链接与那里的代码段不匹配。 Maybe there is a forgotten push ? 也许有一个被遗忘的推动?

Now some ideas on your issue : 现在有关您的问题的一些想法:

  • You haven't set the camera position. 您尚未设置相机位置。 Try : 尝试:

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

  • You don't need to build your sprites manually, libgdx provides features to do that. 您无需手动构建Sprite,libgdx提供了实现此目标的功能。 See Texture packer on the official doc. 请参阅官方文档上的Texture Packer

  • You can also handle resolutions automatically with AssetManager . 您也可以使用AssetManager自动处理分辨率。 See this post for more information. 有关更多信息,请参见此帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM