Right click on the com.norakomi.mambow2 package (or whatever you've named it) and select New -> Class and name it Player. This class will take care of
handling all the logic for our player - what screen coordinate is the ship on and how should it move according to keyboard input - and rendering (drawing) the texture of the ship.
For my ship I will use three textures. One for moving up, one for down and one for flying straight.
Once you've created the class add the following code to it:
Quick analysis of the code:
On line 11+12 we've created a variable for the x and y coordinate and variables refering to the different ship textures.
In the constructor (line 14-19) we set the ship's starting coordinates to mid screen, call the loadPlayerTextures() method which loads the three different ship textures into memory.
This class holds two more methods: render() and update()
Update will be used to process the logic for the player's ship like f.e. move x pixels to the right when we press the right arrow key.
The render method only contains one line of code right now for drawing the ship_middle texture on the x,y coordinate. We will also add more code to this method later on.
Once you've downloaded them drag and drop (or copy/paste) them into the assets folder of your
The badlogic.jpg file is also located there.
Now go back to the MambowGame class and add/change the following highlighted lines of code:
On line 5 we declare a variable player that refers to a Player object, which get initialized at line 15.
On lne 19 I've changed the amount of redness to 0 setting the R, G, B components of the colour that is used to clear the screen all to 0 which results in a black background.
In the render() method on line 22+23 we create a variable delta to store the delta time, which is the time between frame updates. Usually this is
around 1/60th of a seconds if our game is running at 60 fps. We pass this variable to the player's update() method on line 23 where we will use this variable to make
sure that our player's movement will stay consistent independent of whether our frame rate drops or not. We'll go more into that later when we add actually add some code to the player's update()
On line 26 we call the player's render method and pass it the SpriteBatch instance that we created on line 11. The spritebatch is then used inside the player's
render method() to draw the player's ship at the middle of the screen using the ship_middle texture.
If you run the project you should now see something like this:
In the next section we'll continue with adding a class that will take care of handling the keyboard input which will be used to update the player's coordinate's and texture.