简体   繁体   English

libGdx:存在阶段时不会绘制Sprite

[英]libGdx: Sprite is not drawn when there is a stage

I'm sure I'm missing something VERY obvious here, but I'm beginner so don't crush me please. 我确定我在这里遗漏了一些非常明显的东西,但我是初学者所以请不要压垮我。 My problem is that I have a stage that has a viewport small than the screen. 我的问题是我有一个视口小于屏幕的舞台。 Now I also want to draw a Sprite on the screen directly using Sprite.draw(SpriteBatch). 现在我还想直接使用Sprite.draw(SpriteBatch)在屏幕上绘制一个Sprite。 The position of the Sprite and the stage don't overlap. Sprite和舞台的位置不重叠。 The stage is drawn just fine, but the Sprite is not visible. 舞台绘制得很好,但Sprite不可见。 When I comment out the stage.draw() part in the render-method, then the Sprite is visible. 当我在render-method中注释掉stage.draw()部分时,Sprite是可见的。

Code: This is my render-method: 代码:这是我的渲染方法:

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0.851f, 0.894f, 0.992f, 1f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    camera.update();

    stage.act(delta);

    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    stage.draw();
    logoSprite.draw(batch);
    batch.end();
}

Here, I initialize the camera and stage (stageHeight is an int that is just 3/5*the height of the screen): 在这里,我初始化相机和舞台(stageHeight是一个只有3/5 *屏幕高度的int):

camera = new OrthographicCamera();
    camera.setToOrtho(false, SwapItGame.WIDTH, SwapItGame.HEIGHT);
    stage = new Stage();
    stage.setViewport(1080, stageHeight, true, 0, 0, 1080, stageHeight); //The button part of the menu takes up 3 fifth of the Height of hte screen
    stage.setCamera(camera);

Here I initialize my Sprite (The position value for the sprite is rather complex, just ignore it. It's certainly above the stage): 在这里我初始化我的Sprite(精灵的位置值相当复杂,只是忽略它。它肯定在舞台之上):

        logoSprite = skin.getSprite("logo");
        logoSprite.setPosition((SwapItGame.WIDTH-logoSprite.getWidth())/2, (SwapItGame.HEIGHT-stageHeight-logoSprite.getHeight())/2 + stageHeight);

Is is impossible to have a Sprite and a stage on the same Screen? 是不可能在同一个屏幕上有一个Sprite和一个舞台? Or am I doing something fundamentally wrong? 还是我做了一些根本错误的事情?

Try moving 试着移动

stage.draw();

above batch operations 以上批量操作

stage.draw();
batch.setProjectionMatrix(camera.combined);
batch.begin();
logoSprite.draw(batch);
batch.end();

I know this is old, but in the case that you can't or don't want to move your stage.draw() method, just call batch.end() right before your stage.draw(), then restart your batch immediately...I am using separate game screen states for different things in my game and tried to move my stage.draw() around and it only broke other things within my class so I finally just tried this, which worked like a charm and may help someone if the above solution doesn't work easily. 我知道这是旧的,但如果您不能或不想移动stage.draw()方法,只需在stage.draw()之前调用batch.end(),然后重启批处理马上......我在我的游戏中使用不同的游戏屏幕状态并试图移动我的stage.draw()并且它只破坏了我班级中的其他东西所以我终于尝试了这个,这就像一个魅力和如果上述解决方案不能轻松工作,可以帮助某人。

batch.end();
stage.draw();
batch.begin();

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

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