1
\$\begingroup\$

I've encountered a strange problem while trying to develop my game. I deployed the game to my Honor5C and when I open it, a red screen appears, because (I think) only these 2 standard lines of code are functioning properly:

Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

But every other sprite is NOT being rendered. The thing is, if I go back to the phone's main menu, without closing the app, and then I pop it back up again, everything works fine. Here's my render method:

public void render () {
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    cam.update();

    world.step(1f/60f, 6, 2);

    batch.begin();
    batch.setProjectionMatrix(cam.combined);
    bgSprite.draw(batch);

    platformSprite.draw(batch);

    idleSprite1.setPosition((playerBody.getPosition().x * PIXELS_TO_METERS) - 
            idleSprite1.getWidth() / 2, (playerBody.getPosition().y * PIXELS_TO_METERS) - 
            idleSprite1.getHeight() / 2);

    idleSprite1.draw(batch);

    batch.end();
}

What could it be? I've found absolutely nothing googling the issue for an hour straight now. Thank you all.

EDIT @ Charanor

No static variables are being used. Here's the initialization for the variables used in the render method:

batch = new SpriteBatch();

    background = new Texture(Gdx.files.internal("data/bg.jpg"));
    bgSprite = new Sprite(background);
    bgSprite.setPosition(-683f, -384f);

    platform = new Texture(Gdx.files.internal("data/platform.jpg"));
    platformSprite = new Sprite(platform);
    platformSprite.setPosition(-50f, -100f);

    idle1 = new Texture(Gdx.files.internal("data/Idle.png"));
    idleSprite1 = new Sprite(idle1);
    idleSprite1.setPosition(-70f, 0);

    cam = new OrthographicCamera();
    cam.position.set(0,0,0);

    viewport = new StretchViewport(1366f, 768f, cam);
    viewport.apply();

EDIT 2: I've discovered the game still keeps not working if, after launching it, you press the android "back" button and then pop the game back up . The red screen stays. It appears the only way to make it work is to launch the app, press the android button that lets you see what applications are active and switch back to the game from there. It's getting ridiculous.

EDIT 3: I've done some more testing and here's what I found: if you deselect the app and reselect it again either with the recent apps button or the home button we know it works. But, if you press the android back button (it goes back to the phone main menu) after the game is already working fine, and then reopen it, the problem comes back and the screen goes back to full red!

\$\endgroup\$
  • \$\begingroup\$ Are you using any static variables in your code? This might be the leftovers from static variables going out of scope. Can you show the rest of your code? \$\endgroup\$ – Charanor Jul 30 '17 at 19:45
  • \$\begingroup\$ @Charanor added an edit to the post. There's also some Box2D code, but it's basic stuff that shouldn't be causing any trouble... \$\endgroup\$ – Leonardo Formichetti Jul 30 '17 at 20:14
  • \$\begingroup\$ Do you have any code in your resize, pause, or resume methods? \$\endgroup\$ – Charanor Jul 30 '17 at 21:04
  • \$\begingroup\$ @Charanor Nothing. They are implemented from the interface but empty. Like this: public void resize(int width, int height){ } public void resume(){ } public void pause(){ } I'm telling you bro, I usually solve my problems by myself, but this seems so random I had to ask. Thank you btw \$\endgroup\$ – Leonardo Formichetti Jul 30 '17 at 21:07
  • \$\begingroup\$ Show us the following methods show, resize and resume. My bet is you are setting something like the camera/viewport there and that only gets triggered once you return from home. Could also be in the pause or hide methods but it would be counter intuitive to put anything related to the display there. \$\endgroup\$ – Madmenyo Jul 31 '17 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.