[SOLVED]Weird Screen Flicker problem when implementing multiple screens...

Okay guys, another noob question coming…I’ve decided to implement different screens into my brickbreaker game, so I moved all of my game.java code into GameScreen.java and changed the necessary things (created the show method and moved all of create() to it) and i’ve created a splash screen. (splash.java)

I’ve got it working, it loads the splash screen which just changes the screen to white right now and when you click on it, it loads the game from GameScreen. As soon as I click on the screen, it changes but flickers from black to white over and over again like it’s still calling the splash screen…Also, there is a trace of images from the ball that are behind it and none of the bricks are disappearing. I’ve taken a picture which really doesn’t show the screen flashing.

Below is my Splash.java code:


package com.psillicoder.brickbreaker;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class Splash implements Screen {

	private Game MyGame;
	private SpriteBatch batch;
	public boolean isActive;
	
	public Splash(Game g) {
		MyGame = g;
		
	}
	
	
	public void render(float delta) {
		if (isActive) {
		Gdx.gl.glClearColor(1, 1, 1, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		if(Gdx.input.justTouched()) {
            MyGame.setScreen(new GameScreen(MyGame));
            System.out.println("TESTING");
			}
		
		}
	
	}
	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void show() {
		isActive = true;
		
	}

	@Override
	public void hide() {
		isActive = false;
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

}

Below is the show() part of my GameScreen:


@Override
	public void show() {
		isActive = true;
	
		float w = Gdx.graphics.getWidth();
		float h = Gdx.graphics.getHeight();
		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		
		//Initialize objects
		ball = new Ball();
		paddle = new Paddle();
		level = new Level();
						
		camera = new OrthographicCamera(1, h/w);
		batch = new SpriteBatch();
		//Text stuff
		font = new BitmapFont();
		textWidth = font.getBounds(strBricks).width;
		textHeight = font.getBounds(strBricks).height;
		
		//Load level Bricks
		level.LoadBricks();
	}


Didn’t read your code too thoroughly, but maybe try adding camera.update() to your render method? It only helps a bit in my game, but idk about your game - it would be helpful to have the code of the ball, paddle, level.

GameScreen.java
http://pastebin.com/kYv5QhJi

MyGame.java
http://pastebin.com/kYv5QhJi

Splash.java
http://pastebin.com/HtiXFcuM

Level.java
http://pastebin.com/J6c6QezV

Ball.java
http://pastebin.com/G6zVJrQL

Brick.java
http://pastebin.com/bgaCjvpL

It has to be just something small that i’m not doing. I’ve done so many tests and can’t narrow it down to any of my methods.

Anybody have a clue? This is driving my crazy. I’ve tried everything.

I don’t really know when this is called but it looks a little bit odd to me. I guess this is some rendering call. But why are you creating/initializing your stuff there? If that really is a rendering call then you are doing that every frame and that’s not really good. Put your object creation somewhere else like in an init() method or whatever and only update objects that really have to be updated every frame in the show() method.

I’m using libGDX and followed a few tutorials for using different screens. From the tutorial I read, you don’t use the create() method but instead use show() as it’s only called once when the screen is brought to active.

EDIT
Yeah, I added a system.out method and it’s only called once.

show() isn’t the loop, render() is. As for the original problem, you don’t need a render() method in your splash screen, instead try doing something like this in the show() method

super.show();

        // retrieve the splash image's region from the atlas
        AtlasRegion splashRegion = getAtlas().findRegion( "splash-screen/splash-image" );
        Drawable splashDrawable = new TextureRegionDrawable( splashRegion );

        // here we create the splash image actor; its size is set when the
        // resize() method gets called
        splashImage = new Image( splashDrawable, Scaling.stretch );
        splashImage.setFillParent( true );

        // this is needed for the fade-in effect to work correctly; we're just
        // making the image completely transparent
        splashImage.getColor().a = 0f;

        // configure the fade-in/out effect on the splash image
        splashImage.addAction( sequence( fadeIn( 0.75f ), delay( 1.75f ), fadeOut( 0.75f ),
            new Action() {
                @Override
                public boolean act(
                    float delta )
                {
                    // the last action will move to the next screen
                    game.setScreen( new MenuScreen( game ) );
                    return true;
                }
            } ) );

        // and finally we add the actor to the stage
        stage.addActor( splashImage );

(ripped off https://code.google.com/p/steigert-libgdx/source/browse/trunk/tyrian-game/src/com/blogspot/steigert/tyrian/screens/SplashScreen.java). The most important part of this code is how he uses the Action class. I suspect that you may be re-loading the game screen every time a touch is detected.

Ithought that was the case, but I used some debug outputs and it’s only run one time. The splash screen and everything works when I click the screen. It goes to the gamescreen and that’s when the problem starts.

I have to have the render loop in my splashscreen. It’s more like a menu screen. I tried to take the render out and it gave me an error saying i’m missing my render loop.

It’s so confusing. Everything was working fine before I added the different screen. It’s definitely something to do with gameScreen. I just can’t figure out what.


	public void show() {
		//isActive = true;
	
		float w = Gdx.graphics.getWidth();
		float h = Gdx.graphics.getHeight();
		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		
		System.out.println("SHOWN1");

I commented everything else out under show, so this is the only line of code that is being called now and it’s still flickering. If I comment out gdx.gl.glClear(gl20.glcolorbufferbit), the screen just stays white and doesn’t flicker.

There has to be a call im forgetting to do or a variable i’m forgetting to set…It’s gotta be something simple. I’ve searched and searched and searched.

WOOHOO! Solved it. I knew it was something simple. I was calling



			float w = Gdx.graphics.getWidth();
			float h = Gdx.graphics.getHeight();
			Gdx.gl.glClearColor(0, 0, 0, 1);
			Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
			camera = new OrthographicCamera(1, h/w);

in my show method but it needed to be in the render method.

camera = new OrthographicCamera(1, h/w);

certainly should not be in the render method…you never instantiate objects in your game loop. (should be in your constructor/show() method) everything else, yes.

I thought that too, but when I put that part in the show/create method, I get an error on h/w…which normally would make sense, but I don’t set h or w anywhere, so i’m not sure why it works in the render method but not there. i’ll take it out.

EDIT I’m an idiot. Sorry. I completely looked over where I was setting h and w right above it.