If this is the first thing you’ve heard of Basic4Android, then welcome! Welcome to the world of easier Android development. Basic4Android (or B4A for short) makes it much easier and faster to get your apps or games up and running. In this series of posts, I will go through the process of writing a simple Flappy Bird style game, using b4a and the brilliant LibGDX games library.
B4A is not free, but is well worth the investment for the benefits it gives you, you can have a simple app written and running on your phone in minutes! Don’t believe me? Check out this video:
You’ll need a full version of B4A to follow this tutorial, but as I said, it’s well worth the investment, and if you buy it through this site, then you will save 30% off the full price!
Anyway, let’s get on with the tutorial…..
by the end of this series, you’ll have written a game like shown in the video below, and have the skills to change it to what ever you want!
Part 1 – Drawing an image
What you need
LibGDX library – Download here
Phone / Tablet that runs Android 3.0 and up
It will be much easier for you to write games that just support one orientation, the LibGDX engine can handle the orientation changing, but for simplicity we will stick to one orientation. The Flappy Bird game using portrait, so for our tutorials we will also use portrait.
Let’s exclude the title bar for the activity, you never see this in games so we don’t want to see it in ours. We’ll keep the status bar at the very top of the screen though so leave the FullScreen option set to false.
We don’t need to declare anything here as our entire game will just be within one module.
This is where we declare our game veriables, like score, any types we’re using, and anything else we’ll mwant to refer to. This is also where we declare our LibGDX objects.
Dim myBacking As lgTexture
Declaring myBacking as a texture creates a texture in the devices graphics memory, this is where we will store our background image.
Dim surface As View
The surface is where we will draw our libGDX accelerated graphics. We will spread this surface object across the whole screen before we draw on it.
Dim vpW As Float = 720
Dim vpH As Float = 1134
We want to be able to draw to the same screen coordinates to make it easier to write our games. So we will set up a viewport for the camera with the above dimensions. We can then say that drawing to something like 310x by 560y will be around the middle of the screen no matter what device screen we’re running on.
We now setup the LibGDX objects themselves
Dim lGdx As LibGDX
This line tells B4A to load the LibGDX library, giving us access to the whole LibGDX command set, for drawing our graphics, playing sound effects and handling inputs.
Dim GL As lgGL
This line’s required due to LibGDX using OpenGL to accelerate the graphics functions.
Dim Camera As lgOrthographicCamera
This is our camera that looks at our surface we’re drawing to in order for us to see the graphics we’re drawing. Cameras can be zoomed in and out, rotated and moved around just like a real camera.
Dim Batch As lgSpriteBatch
When drawing our graphics, we’ll use a batch section, this enables LibGDX to draw all the graphics at once which is much faster, and helps LibGDX games run smoothly on older devices.
This is where we initialise our LibGDX graphics engine and tell Android where to draw it
surface = lGdx.InitializeView(“LG”)
The above line has changed after a recent library update, if you are running version 0.95 or earlier of the libGDX library then use: surface = lGdx.InitializeView(true,”LG”)
Activity.AddView(surface, 0, 0, 100%x, 100%y)
So we initialise the surface object as a lGdx view with the name LG. We then add this view to the activity filling the whole screen excluding the status bar as the activity property is set to non-fuillscreen.
This simply tells the LibGDX engine to resume running if it’s been initialised previously.
Here we tell the LibGDX engine to pause if it’s been initialised previously.
This is where we load any graphics or sounds for our game and initialise any other objects we need. In our example here we Initialise the sprite batch for drawing graphics in our main render method, we also load the png graphics file into our myBacking texture ready to draw to the screen.
This file must exist in your files folder within the project folder. The file can be any size and resolution as LibGDX will scale it to match what ever scale we draw it to. Obviously, the smaller the image then the less memory it will take to draw it. So if you’re planning on a game that has a lot of graphics then you’ll want to keep those graphics file sizes down.
This is called when the surface changes size, mostly this is when the orientation changes. As we’ve set our activity to only support portrait then this won’t be called at all.
This is the meat of our game! This is where we draw the actual game to the screen!
We start by clearing the screen
We set the clear colour, this is set using Red,Green,Blue,Alpha values between zero and one. So here we are just setting it to a totally black screen with no transparency. We then call the command to clear the screen.
We call the camera update command.
Set the batch projection matrix to match the camera combined type. I’m not going to try to explain how this line works, as I don’t honestly, know, all I can say is it makes the graphics commands we use in our batch match the screen coordinates of the viewport we configured earlier.
We then start the batch section to draw the graphics.
This line tells LibGDX to draw the myBacking image to the screen, starting at 0,0 (the bottom left of the screen) to the top left of the screen at vpX,vpY
We then finish the batch.
There is then the LG_Pause, LG_Resume, and LG_Dispose methods are declared to handle those events. We’re not covering using these in this tutorial.
Proceed to the next tutorial: