简体   繁体   English

Spritebatch没有绘制任何LibGDX

[英]Spritebatch not drawing anything LibGDX

This is one of the oddest things that has ever occured in LibGDX for me. 这是我在LibGDX中发生过的最奇怪的事情之一。 I have used these exact specifications for all my other States in my game, but under different names and they all work fine, except for my ShopState, which won't render ANYTHING at all! 我已经在我的游戏中使用了所有其他状态的这些确切的规格,但是在不同的名称下它们都工作正常,除了我的ShopState,它根本不会呈现任何东西! Here's my code for the class: 这是我的课程代码:

public class ShopState extends State{
    private Texture bg;
    private Sprite shopLayout;
    private OrthographicCamera shopCam;
    Viewport viewport;

    public ShopState(GameStateManager gsm) {
        super(gsm);
        shopLayout = new Sprite(new Texture(Gdx.files.internal("shopLayout.png")));
        bg = new Texture("bg2.png");
        shopCam = new OrthographicCamera();
        viewport = new StretchViewport(720, 1280, shopCam);
        viewport.apply();
        shopCam.position.set(shopCam.viewportWidth / 2, shopCam.viewportHeight / 2, 0);
        shopLayout.setPosition(shopCam.viewportWidth / 2 - shopLayout.getWidth() / 2, shopCam.viewportHeight / 2 - shopLayout.getHeight() / 2);
        shopLayout.setSize(650, 1100);
    }

    @Override
    public void handleInput() {

    }

    @Override
    public void update(float dt) {
        handleInput();

    }
    @Override
    public void resize(int width, int height){
        viewport.update(width, height);
        shopCam.position.set(shopCam.viewportWidth / 2, shopCam.viewportHeight / 2, 0);
    }


    @Override
    public void render(SpriteBatch sb) {
        shopCam.update();
        sb.setProjectionMatrix(shopCam.combined);
        sb.begin();
        sb.draw(bg, 0 , 0, shopCam.viewportWidth, shopCam.viewportHeight);
        shopLayout.draw(sb);
        sb.end();

    }

    @Override
    public void dispose() {
        bg.dispose();
        shopLayout.getTexture().dispose();

    }
}

What am I doing wrong? 我究竟做错了什么? Everything seems fine, but all I get when I click on the Shop button, it gives me a black screen! 一切似乎都很好,但是当我点击商店按钮时,我得到的只是黑屏!

Ok, found a solution, but I have no idea why this fixes it. 好的,找到了解决方案,但我不知道为什么要修复它。 All I had to do was add a stage that uses the current viewport. 我所要做的就是添加一个使用当前视口的舞台。

stage = Stage(viewport);

And that was it! 就是这样! It is now working properly, for some reason... 由于某种原因,它现在正常工作......

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

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