Creating Screens

Screen Class

  1. We’ll first start by making a new screen class. To do this make sure to first make a screen package(which is really just a folder) called Screens.

2. Inside of this package you will create a new Java Class and call it whatever you want your first level to be called.

3. Once you have a new class you must make sure that it implements the Screen interface, which will help Libgdx know that this class is meant to act as a Screen for your Game. Make sure that the import statement is present. Having Android Studio auto-complete things for your will be helpful in importing classes.

import com.badlogic.gdx.Screen;

public class LevelOne implements Screen
{

}

4. You must make sure that you include all of the necessary Screen methods. Hover over the class declation and click on “Implement Methods”. Click “ok” to implement all necessary methods.

Your class should now look like this:

public class LevelOne implements Screen
{

    @Override
    public void show()
    {

    }

    @Override
    public void render(float delta)
    {

    }

    @Override
    public void resize(int width, int height)
    {

    }

    @Override
    public void pause()
    {

    }

    @Override
    public void resume()
    {

    }

    @Override
    public void hide()
    {

    }

    @Override
    public void dispose()
    {

    }
}

Using the Batch

We should only have one SpriteBatch since it is very expensive, computing-wise, to switch batches at any one time. To share the Game’s batch we need a method that will help us do that. Add this method to your Game class.

public SpriteBatch getBatch()
{
	return batch;
}

With this method we will be able to access the batch from any other class, as long as we have access to the Game class. Batches are like boxes that store all of the graphical components of our game. We want to have one batch be available to the graphics card and we can open and close that batch at different times. We don’t want to create a new batch and switch it because our game might get laggy as this will take a long time to happen.

To be able to access the batch from the Screen class, we need to provide a variable and pass it in through the constructor. This means that the class will save access to the game whenever it is created.

In your Screen class:

DemoGame myGame;
    
public LevelOne(DemoGame game)
{
    this.myGame = game;
}

then in render() we can open, store stuff and close the batch.

@Override
public void render(float delta)
{
    myGame.getBatch().begin(); //open batch
        
    myGame.getBatch().draw( /* stuff here */);  //store things
        
    myGame.getBatch().end(); //close the batch
}

Creating a Texture

A texture is a graphical component in Libgdx. For this demonstration we are going to create a texture with a picture of type .jpg. See documentation here.

    Texture myPicture;

    public LevelOne(DemoGame game)
    {
        this.myGame = game;
        myPicture = new Texture(/* name of asset here */);
    }

    @Override
    public void render(float delta)
    {
        myGame.getBatch().begin(); //open batch

        myGame.getBatch().draw( myPicture, 0, 0);  //draw your texture at x coordinate 0 and y coordinate 0

        myGame.getBatch().end(); //close the batch
    }

Clean up Game

Remove old code from the setup in Game and create a new Screen. You MUST use setScreen() to set the new screen to what should be shown.

Don’t forget to use super.render() in Game’s render class!

Game class:

        SpriteBatch batch;
	Screen currentScreen;
	
	@Override
	public void create () {
		batch = new SpriteBatch();
		
		currentScreen = new LevelOne(this);
		setScreen(currentScreen);
	}

Video Lesson