-
Notifications
You must be signed in to change notification settings - Fork 0
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.
//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() {
}
}
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.
//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);
}
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.
//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");
}
}
}
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.
//part of a simple snake game
public void renderLoop() {
g.setColor(Color.yellow);
g.drawString("Score: " + score, 20, 20);
}
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.
Entity obj:
The entity you want to add to the scene.
//Create a new player (must implement the entity interface)
Player player = new Player();
//add the player to the scene
addObject(player);
This method is used to remove an entity from the scene.
Entity obj:
The entity you want to remove.
//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);
This method is used to remove all entitys from the scene.
//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();
Returns a list List<Entity>
that contains all entities in the scene.
Returns a specific entity from the scene.
int index:
The position in the ObjectList
of the object you want to get.
The display this scene is attached to.
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.
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.