Skip to content

Scene class

Pegacraffft edited this page Jan 14, 2021 · 6 revisions

Scene class

This is an abstract class. Its used to make a new class inherit the scene class, so it can be used as scene.

Example:

//The class with the implementation of the scene class
public class GameScene extends Scene {
    @Override
    public void init() {
    }

    @Override
    public void logicLoop() {
    }

    @Override
    public void renderLoop() {
    }
}

1. init()

This abstract method gets executed once after the scene got attached to a display. It is most commonly used to add objects to your scene and construct how the scene should look like.

Examples:

//part of a simple snake game
public void init() {
        score = 0;
        player = new Snake((display.getHeight() / 20) * 10, (display.getHeight() / 20) * 10);
        createFruit();
        addObject(player);
        addObject(player.tale);
    }

2. logicLoop()

This abstract method gets executed 60 times per second (if you didn't change the frame rate with Game.setFrameRate()) This loop is most commonly used for the logic in a scene, that cant be handled by the objects themselves.

Examples:

//part of a simple snake game
public void logicLoop() {
        if (player.x < 0 || player.x > display.getWidth() - 30 || player.y < 0 || player.y > display.getCanvas().getHeight() - 10)
            display.attachScene("Menu");


        if (player.h.isInside(f.h)) {
            score++;
            createFruit();
            player.addTale();
        }
        for (int i = 2; i < player.tale.getEntityList().size(); i++) {
            Tale t = (Tale) player.tale.getEntityList().get(i);
            if (player.h.isInside(t.h)) {
                display.attachScene("Menu");
            }
        }
    }

3. renderLoop()

This abstract method gets executed 60 times per second (if you didn't change the frame rate with Game.setFrameRate()) This loop is most commonly used for the rendering graphics, that cant be shown by the objects themselves.

Examples:

//part of a simple snake game
public void renderLoop() {
        g.setColor(Color.yellow);
        g.drawString("Score: " + score, 20, 20);
    }

4. addObject(Entity obj)

This method is used to to add an entity to the scene. This is one of the most important methods for designing a scene. No entity will be shown automatically if this method is not used.

Parameters:

Entity obj: The entity you want to add to the scene.

Examples:

//Create a new player (must implement the entity interface)
Player player = new Player();
//add the player to the scene
addObject(player);

5. removeObject(Entity obj)

This method is used to remove an entity from the scene.

Parameters:

Entity obj: The entity you want to remove.

Examples:

//Create a new player (must implement the entity class)
Player player = new Player();
//add the player to the scene
addObject(player);
//remove the player from the scene
removeObject(player);

6. removeAll()

This method is used to remove all entitys from the scene.

Examples:

//Create a new player (must implement the entity class)
Player player = new Player();
//add the player to the scene
addObject(player);
//remove all entities
removeAll();

Getter and setter:

getObjectList()

Returns a list List<Entity> that contains all entities in the scene.

getObject(int index)

Returns a specific entity from the scene.

Parameters:

int index: The position in the ObjectList of the object you want to get.

Public variables:

display

The display this scene is attached to.

keyListener

This variable is the keyListener of the display. To find out more about keyListers click here.

It is used if you want to interact with the keyboard in your program.

mouseListener

This variable is the mouseListener of the display. To find out more about mouseListener click here.

It is used if you want to interact with the mouse in your program.