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



Nutrition & Food |Learn Travel Spanish Free | Electric Kick Scooters - News, HowTos, Todays Deals!


Homepage | Tutorials | Blog



March 2025

Libgdx with Android For Game Dev

Game Dev Tutorial :Add Sound To The Coin Each Time it Increments
Game Dev -All Game Dev Tutorials

App Project Name: Add Sound Game Coin



In the previous tutorial, we learned how to create a separate game class for all the coin logic so that we could add it to the Stage as an Actor. We wanted to add to the Stage, because once it is added to stage as an actor, we can then add actions to it, just like a game character. The CoinActor class was created with 'public class CoinActor extends Actor'. The CoinActor class moved the coin(and circle) around the game screen, and then it moved off the game screen.

With this tutorial, we are going to add Sound to that same coin, and also add some logic, so that the coin will increment each time it moves off the game screen. And, we are going to add a Label for the coin action. The ground(at bottom of screen where the text displays) will at first display the word 'Crypto Wallet', however, once the first coin is added to the Wallet, the text will change and instead display the word 'Bitcoin $', along with the amount of the bitcoin added. So, after the first loop of the coin it will display, 'Bitcoin $1', and increment with each additional loop it makes. Each time the coin is re-added to the wallet,(each loop), the bitcoin will increment by 1 dollar.

All the other game code is exactly the same as the previous tutorial, therefore, in this tutorial, we only discuss adding the Sound, the Label, and the coin incrementation.


All code for this tutorial is at the Example Code section on this page. At App Shot, you can download the 'coinTally_sound.apk', and install it on your Android device to see the action.

FYI: The code from this game dev tutorial is compatible with Android Studio with the Libgdx component. Read more about Android Studio and Libgdx Here

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, Add Sound Game Coin(Note: no spaces in the app name(can use _ ), or 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.add_sound_coin'.

Click Create; Add Sound Game Coin game app is created, and the MyGdxGame.java page opens in your AIDE coding editor.


For more info on navigating files and using the AIDE coding editor, goto Tutorial 1.



App Shot - Portrait 5.8" Game Screen
Add Sound Game



This app is available for download.
Download/Install This App to See the Action: Here
and, can also view this video at this link: Our Game Dev Videos(shorts) on Google


Code For This Tutorial

For this tutorial, we must include the imports, game class, class members(with variables), create method, render method, dispose method. And, include any additional classes and methods we require for this tutorial. Since the code for this tutorial is exactly the same as the previous tutorial except for the code for the Sound, Label, and int(coinTally), we need only discuss those class members.

Imports and Game Class - Imports and game class same as previous tutorial.

Variables At variables we include the class members and then our named variables for each of them. So, for this tutorial, we include the class members and their variables for the Sound, Label, and Int. Sound we name our variable as coinSound, Label we name our variable as coinTallyLabel, and int, we name as coinTally= 0;, meaning the count starts at 0. All other class members are the same as the previous tutorial.
Also, note that the coin sound variable is added at the CoinActor class code; whereas the int and label variables are added with the game class code variables. Including variables with game class or the core class of your game means they can be called in other areas of your game, like perhaps, another game screen. However, if you include them only in a specific game class like our CoinActor class, then they can only be called within that code. Here the Sound variable is going to be called in the CoinActor class instance only, so we include it with the CoinActor code. The Label member however, is coded at the game class variables meaning, it can be called(coded) in another screen, like a end of game screen that shows the final score or a final tally. The int variable is also coded at game class because it is required to display the info for the coinTallyLabel.

Here is the screenshots of the class members and variables for this tutorial.
libgdx add sound to coin action
libgdx add sound to coin action

Create - At create method, we include the new instances of all the class members we are going to implement. We want to create the Label, and its' variable name - coinTallyLabel, so we add code for that. We create it, and add some text for the label, and we position it on the game screen. For the text we use 'Crypto Wallet'.
libgdx, create a game label
For the Sound member, and, its' variable - coinSound, we load the sound file, a mp3 file named - coin1.mp3, and create the instance of the coinSound, in the CoinActor class code, shown in this next image.
libgdx, create a game label

For for class member - 'int',and its' variable , coinTally, we can add it directly to our action code; 'coinTally++' means it will increment with each loop, and setText means to add the words 'Bitcoin $', and '+coinTally', means start the count at 0; 'coinSound play', means to start playing the mp3 file, and .06f, is the volume for the sound. This can be adjusted as you want. So, as the coin moves off the game screen, all these actions begin; the name changes from 'Crypto Wallet' to 'Bitcoin $', the amount changes from 0 to $1,and the Sound is heard just as the coin moves off the game screen, into the wallet(assumed crypto wallet).
libgdx, create a game label

Render - Here, we can simply use 'stage.draw', to draw our coin action to the game screen. Because we created the CoinActor class, and added it as an actor to the stage; we can use stage.draw to easily draw all its' action to the game screen.

Render method, is where we draw our game graphics to the screen. So this would include images, fonts, game characters, and the like. If we using Stage, we can draw them to stage with the 'stage.draw()' code call, otherwise, we can draw them with batch.

Dispose - At dispose method, we get to dispose of all our game assets, to keep our game performance at its' best. If using Stage, we can dispose of all assets at once, with 'Stage.dispose();' code call, otherwise we can dispose of each variable directly.



Summary

In this tutorial, we added Sound to the CoinActor action, which has a single coin falling on the screen and then off the screen into a Crypto Wallet. Each time the coin moves off the game screen, it will reset itself and continue the loop again. By adding the coinTally logic to the coin loop, the coin tally increments the bitcoin amount by one dollar. We also included a Label in the action, that allowed us to change the name from 'Crypto Wallet', to, 'Bitcoin $', once the action scene for the sound commences.

With Stage and Actor of Libgdx, it is easy to action your game characters and game assets. When you write game logic for your game action scenes, you can re code it into a separate game class that extends Actor; that way, that same code can be defined as an Actor and added to the Stage. And, because you re wrote it into a separate Actor class, that class can be easily added into any game code you might create in another game app.

Game characters, objects, and game assets don't have to be added as actors. In fact, you don't have to implement Actor and Stage at all if you don't want to, and you can create a mobile game without using either in Libgdx. But, if you plan on having lots of actions, and interactions amongst them, Actor and Stage of Scene2d, do provide a simple and straightforward approach to create complexity in game action scenes. Additionally, by coding a 'Runnable' action, as we did in this tutorial, you can easily add game logic, sounds, and labels in with your action class code.


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;
One sound file: 'coin1.mp3'
One game asset file: 'bitcoin88-reb.png'
Download/Save each to your device and add to the Assets folder in the AppProject hierachy at:

AppProjects/addsoundtogameaction/Gdx-game-android/assets/coin1.mp3

Download/Save the sound file. Click Here

Save the bitcoin image.
image libgdx




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