简体   繁体   English

除非Android应用程序暂停,否则精灵不会显示-LibGDX

[英]Sprite not displaying unless android app pauses - LibGDX

Here is the code: http://hastebin.com/ugufakeyap.java 这是代码: http : //hastebin.com/ugufakeyap.java

When I run this, the sprite "aspectRatios" is not displayed on the screen. 当我运行此命令时,屏幕上未显示精灵“ aspectRatios”。 But when I pause the program by pressing my home button and then resuming the program, it displays the sprite onto the screen. 但是,当我按下主屏幕按钮暂停程序,然后继续执行程序时,它将在屏幕上显示精灵。 Why is this? 为什么是这样? It's really annoying. 真烦人。

you need to call camera.update(); 您需要调用camera.update(); after changing position. 换位置后。 My best guess is that libGDX automatically updates the camera on resume. 我最好的猜测是libGDX在恢复时会自动更新相机。

public void create () {
    batch = new SpriteBatch();
    txt = new Texture(Gdx.files.internal("test2.png"));
    aspectRatios = new Sprite(txt);
    aspectRatios.setPosition(0,0);
    //aspectRatios.setSize(100,100);

    camera = new OrthographicCamera();
    viewport = new StretchViewport(1080,1920,camera);

    camera.position.set(camera.viewportWidth/2,camera.viewportHeight/2,0);
    camera.update();
    viewport.apply();
    //batch.setProjectionMatrix(camera.combined);
    Gdx.input.setInputProcessor(this);
}

You would normally update the batch projection matrix there as well but you do it every frame in the render method anyway. 通常,您也可以在那里更新批处理投影矩阵,但是无论如何,您都要在render方法的每一帧中进行更新。

Some people helped me on the libGDX irc channel . 有人在libGDX irc频道上帮助了我。 It turns out that I wasn't calling 原来我没打电话

viewport.update(width, height, centerCamera);

(Thanks to Xoppa ) (感谢Xoppa

And (according to ASneakyFox ): "you have to assume that the android device isnt going to be conveniently the size you want, itll call resize when the app opens up or changes orientation so you can modify your layout to be the dimensions of the screen (ie change the viewport)" 并且(根据ASneakyFox ):“您必须假设android设备不会达到您想要的大小,它会在应用打开或更改方向时调用resize,因此您可以将布局修改为屏幕尺寸(即更改视口)”

Here is the updated version of the code: http://hastebin.com/olivopocix.java 这是代码的更新版本: http : //hastebin.com/olivopocix.java

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

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