Check out part 1 for the project and world setup!
Check out part 2 for implementing controls for our runner!
We introduced our enemies on part 3.
We rendered our background and ground on part 4.
Remember the code is on GitHub. Graphics were made by Kenney. Also, a final version based on this tutorial is on Google Play.
Where Are My Characters?
If are coming from the previous part, you probably know that we stopped showing our runner and the enemies but hey, at least the background and the ground are looking good! In this next part we are going to display our characters again.
Let’s start with the runner (I know he’s not the same as the one on the final version, sorry!). So, we have four states for our guy: he’s either running, jumping, dodging or too busy getting hit by the enemies. We are going to store all his textures, along with the enemies’, in a texture atlas. Here is the image (characters.png):
And the following is the texture file (characters.txt):
So if you are new to this you are probably asking yourself “What kind of sorcery is this?”. Well, I used TexturePacker to put together my .png files (they have an option for libGDX textures) and the two files were automatically generated. LibGDX also has its own TexturePacker but I’ve never tried it. My guess is that it would work even better for this game. Both of these files are stored in the assets directory.
The texture regions when the runner is hit, dodging or jumping are easy to get since they consist of a static frame (alienGreen_hit, alienGreen_dodge, and alienGreen_jump), we set a separate TextureRegion for each state and apply it inside our draw function. For the running state, we have two frames (alienGreen_run1 and alienGreen_run2) which we’ll use to create an Animation.
First, let’s take note of all this in our Constants class:
And in our Runner class, let’s assign these TextureRegion objects along with the running Animation.
That’s all there is for animating the Runner. Run the project and see the guy run and get hit by the enemies we still don’t see :D. The video below shows how the game looks so far:
Know Thy Enemy
I will take advantage of our EnemyType enum to store all the texture region names needed for each Enemy. They will be very easy to render now that we know how to render an Animation. Each enemy has its own sprites, I will set them as follows:
Running small - lady bug
Running wide - worm
Running long - barnacle
Running big - spider
Flying small - bee
Flying wide - fly
Let’s add these values in our Constants:
And as I mentioned before, use EnemyType to store these values:
Our EnemyUserData should now store the region names the Enemy should render. We’ll pass the texture regions set in the constructor:
And modify our EnemyUserData instantiation inside WorldUtils:
Now we have all the assets we need to render the enemies! The logic to render them will be fairly similar to the logic we used to render our Runner with one exception. I’m rendering a bigger rectangle than the actual physics body (20% wider, 10% higher). The purpose of this is to make a little bit of overlapping possible between the runner and enemies so the hits look more real. The changes are the following:
Run the game and now you will see your fierce enemies coming towards you! Jump and dodge to avoid getting hit by any of them! The game should be working like in the video below (ignore the title, the original game name was modified before the first release):
Finally, I’d like to go back to our Runner drawing and apply the same ratio we just applied to our enemies (only 20% wider, though) so the overlapping also applies nicely to our Runner:
Run the game one last time to make sure everything is working as expected (ignore the title, the original game name was modified before the first release):
That’s a Wrap!
Congrats on making it this far! I hope that this guide helped you understand a little better the basics of libGDX, using box2d and scene2d, and adding textures to your game. There are probably many things I could’ve done better, but I’m satisfied with the final product for teaching purposes.
I added more stuff to the game such as UI controls for menus, playing music and sounds, integrated Google Play Game Services as well as tweaked it a little bit to gradually increase the speed of the game. That’s fairly easy to implement and I think you can do it following libGDX’s wiki and other tutorials. Look at the final code to see how I did it.
Thanks for reading. If you liked this tutorial, I would really appreciate it if you could help spread the word by sharing it.