对我来说,这是LibGDX中发生过的最奇怪的事情之一。我在我的游戏中所有其他州都使用了这些确切的规格,但是使用了不同的名称,它们都可以正常工作,除了我的ShopState之外,它根本不会呈现任何内容!这是我的课程代码:
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();
}
}
我究竟做错了什么?一切似乎都很好,但是当我单击“商店”按钮时,我得到的一切,它给了我黑屏!
最佳答案
好的,找到了解决方案,但我不知道为什么要解决它。我要做的就是添加一个使用当前视口的舞台。
stage = Stage(viewport);
就是这样!由于某种原因,它现在可以正常工作...