Game Dev with Android
A{} N{} D{} R{} O{} I{} D
Game Dev with Libdgx





Homepage | Tutorials | Blog



March 2024

Libgdx with Android For Game Dev

Game Dev Tutorial : ImageButton Stage
Game Dev -All Game Dev Tutorials

An ImageButton can be created with the Scene2d of Libgdx. First we add the texture that will be our imageButton in this case, our .png: gamePlay.png Then we add our imageButton variable, and instance of it, and then we add the texture 'gameplay.png' to the imageButton, and then we set that as our Actor. Then we add the actor to the stage. And lastly, we draw the stage, which in turn, draws our imageButton to the screen. Code and image for this tutorial is at the Example Code section on this page.
Because we want our ImageButton to scale properly across our Portrait and Landscape orientation, we are going to use an 'ExtendViewport' and add the 'Orthographiccamera' to code also.



Create our Game App Screen


We are creating a new app for this tutorial.
First, we must create our new mobile game app, so in AIDE, at the AppProject file hierachy, Choose Create New Project, select Mobile Game, and New Mobile Game; then add the App name, ImageButtonStage(Note: no spaces in the app name(can use _ ), you can add spaces after you create it; Just Goto: the 'AppProject' folder; just select the App Name, and then select rename). Next, add the package name, 'com.aac.imagebutton_example'.

Click CREATE. Now ImageButtonStage app is created, and the MyGdxGame.java page opens in your AIDE coding editor.

App Shot - ImageButton Stage
Screen Size 5.8"
Stage, code imageButton
Landscape orientation
Stage Libgdx, imageButton code

Code For This Tutorial

Imports Code
First we add the imports required for the variables we are going to add and our methods. So we add 'texture, imagebutton, stage, spritebatch, textureRegionDrawable, viewport, ExtendViewport, ApplicationAdapter, OrthographicCamera, as shown in this image.

Image Button Stage imports

Variables Code - we must declare variables for the calls: stage, imageButton, texture, extendViewport, orthographic camera, and, spritebatch. First we declare our code calls, then we create a variable for each of them. You can name them as you like.

Image Button Stage variables

At the Create method code, we create new instances of the variables. We add our game's image, using the Texture method, and then set it as the ImageButton. Then we set the actor from our imagebutton, and then add actor to the stage. Also in create method, we can set the position and size of our image button as shown in this next image.

Image Button Stage Create method

Render method code - at our 'render' code we include the 'stage.act' and 'stage.draw' methods to draw our ImageButton to the game screen. We also update our viewport and camera with the 'camera.update', and 'setProjectionMatrix', and 'resize' calls. By adding these calls, our game screen will display properly in both portrait and landscape modes, and across other device screen sizes. Do note; this may require additional code tweaking depending on the complexity of your game screen. And, we also include our screen color code in the 'render' method. For this tutorial, we added 'rgb' color codes.
Image Button Stage Render method

In the 'dispose' method, we dispose of the stage and texture.
Image Button Stage Dispose method

Summary

In this tutorial, we implemented an 'ImageButton' with the stage and actor methods. We included an 'orthographic camera', and 'Extendviewport', so our game would scale properly across different device screen sizes.


EXAMPLE CODE - Code For This Tutorial.

JUST copy and paste to update your code.

To Select all Code, with Notations
CLick here


App Project Images; This tutorial has one image.
Save this image: gamePlay.png,(332x186) and add to 'assets' folder of the app project; ImageButtonStage, at:
AppProjects/ImageButtonStage/Gdx-game-android/assets/gamePlay.png



AndroidAppCoding.com - GameDev, All Rights Reserved.
All images posted on this Website are Copyrighted © Material.