Building a 3D Game with LibGDX by Sebastian Di Giuseppe & Andreas Krühlmann & Rijnswou. Elmar van
Author:Sebastian Di Giuseppe & Andreas Krühlmann & Rijnswou. Elmar van [Giuseppe, Sebastian Di]
Language: eng
Format: azw3
Publisher: Packt Publishing
Published: 2016-08-29T04:00:00+00:00
Screens
We already have a screen that renders the game, and all it needs to work and have gameplay. Now, we will need to add other common screens (unless you go with an original design). For our game, we'll need: a main menu screen, a loading screen (to avoid weird freezes), and a leaderboards screen.
Main menu screen
Our main menu screen needs to have some basic items such as a background, a title, a play button, a leaderboards button, and a quit button. We won't get into more details because we are building a prototype so we'll only include the primary features.
We will first change Core.java to make it read our main menu screen before launching the actual game screen:
Core.java to make it read our main menu screen before launching the actual game screen: public class Core extends ApplicationAdapter { ... @Override public void create() { ... setScreen(new MainMenuScreen(this)); } ... }
Let's create a class called MainMenuScreen.java and place it on our screens package:
public class MainMenuScreen implements Screen { Core game; Stage stage; Image backgroundImage, titleImage; TextButton playButton, leaderboardsButton, quitButton; public MainMenuScreen(Core game) { this.game = game; stage = new Stage(new FitViewport(Core.VIRTUAL_WIDTH, Core.VIRTUAL_HEIGHT)); setWidgets(); configureWidgers(); setListeners(); Gdx.input.setInputProcessor(stage); } private void setWidgets() { backgroundImage = new Image(new Texture(Gdx.files.internal("data/backgroundMN.png"))); titleImage = new Image(new Texture(Gdx.files.internal("data/title.png"))); playButton = new TextButton("Play", Assets.skin); leaderboardsButton = new TextButton("Leaderboards", Assets.skin); quitButton = new TextButton("Quit", Assets.skin); } private void configureWidgers() { backgroundImage.setSize(Core.VIRTUAL_WIDTH, Core.VIRTUAL_HEIGHT); titleImage.setSize(620, 200); titleImage.setPosition(Core.VIRTUAL_WIDTH / 2 - titleImage.getWidth() / 2, Core.VIRTUAL_HEIGHT / 2); playButton.setSize(128, 64); playButton.setPosition(Core.VIRTUAL_WIDTH / 2 - playButton.getWidth() / 2, Core.VIRTUAL_HEIGHT / 2 - 100); leaderboardsButton.setSize(128, 64); leaderboardsButton.setPosition(Core.VIRTUAL_WIDTH / 2 - playButton.getWidth() / 2, Core.VIRTUAL_HEIGHT / 2 - 170); quitButton.setSize(128, 64); quitButton.setPosition(Core.VIRTUAL_WIDTH / 2 - playButton.getWidth() / 2, Core.VIRTUAL_HEIGHT / 2 - 240); stage.addActor(backgroundImage); stage.addActor(titleImage); stage.addActor(playButton); stage.addActor(leaderboardsButton); stage.addActor(quitButton); } private void setListeners() { playButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { game.setScreen(new GameScreen(game)); } }); leaderboardsButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { game.setScreen(new LeaderboardsScreen(game)); } }); quitButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { Gdx.app.exit(); } }); } @Override public void render(float delta) { /** Updates */ stage.act(delta); /** Draw */ stage.draw(); } @Override public void resize(int width, int height) { stage.getViewport().update(width, height); } @Override public void dispose() { stage.dispose(); } // empty methods from Screen }
We'll start implementing the Screen interface on our class, because, as you know, this is a screen. We will save a Core instance to be a global variable, then create a new Stage object that will contain our new widgets for this respective screen, which is a backgroundImage, a titleImage, a playButton, a leaderboardsButton, and a quitButton.
Let's create a constructor for the class and have a Core game parameter to pass by our game instance, then save it. We'll instantiate the Stage object with a FitViewport again, with our static VIRTUAL_WIDTH and VIRTUAL_HEIGHT on the Core class.
The SetWidgets() method again will instantiate our widgets with our resources (skin, images, and font). Configure your widgets on the respective method.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Mikado Method by Ola Ellnestam Daniel Brolund(26560)
Hello! Python by Anthony Briggs(25464)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(24716)
Kotlin in Action by Dmitry Jemerov(23798)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(23119)
Dependency Injection in .NET by Mark Seemann(22887)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(21628)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(20486)
Grails in Action by Glen Smith Peter Ledbrook(19535)
Adobe Camera Raw For Digital Photographers Only by Rob Sheppard(17056)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(16511)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(14169)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(12355)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(11624)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10645)
Hit Refresh by Satya Nadella(9223)
The Kubernetes Operator Framework Book by Michael Dame(8579)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8432)
Robo-Advisor with Python by Aki Ranin(8376)