If you want to port your javax.swing
game to libgdx you have to know how you made your game. Did you separate the logic and the view? If yes, fine you only have to rewrite the view. If not, well, its maybe better to start from scratch.
First basics to know: The entrypoint to your game is the ApplicationListener
Interface. If you don't need every method inside it, extend Game
, which gives you some deffault behaivor. So i would suggest to use that. The Game
or ApplicationListener
now gives you possibility to react on some application events:
create()
: called when your app is started
dispose()
: called when your game is closed (not crashed :P)
pause()
: called on android, when call is incoming/home button is pressed. On desktop when you minimize window/when the game lost the focus.
resume()
: called when you come back after pause state.
resize()
: called when you resize the window and on android, when screen rotates (you have to allow it in the manifest file)
render()
: called every gameloop (if enabled), used to update the objects in game and then draw them to the screen.
The default render() implementation in the Game
class calles render for your current Screen
.
Screen? Whats that?
Well think about a normal game. You start it and it showes a menu, with some buttons like Start Game
, Settings
... This is the Menu screen. So Screen
s are parts of your game with different logic and view, so that they have to be in sepparate classes. Screen
s implement the Screen
interface, which again gives you some useful methods:
show()
: called, when you set this Screen
as the Game
s Screen
with MyGdxGame.setScreen()
.
hide()
: called, when another Screen
is set as the Game
s Screen
.
So if the current Screen
is the MenuScreen
and i press the Play Game
button, show()
for the PlayScreen
is called and hide()
for the MenuScreen
is called.
The Screen
also has the methods pause()
, resume()
and resize()
, which are called if the same methods in Game
are called (always only for the current Screen). If you implement ApplicationListener
you have to write this default behaivor yourself.
Note, that dispose()
for the Screen
is not called automatically.
Next thing is the drawing. For drawing in 2d there are the:
ShapeRenderer
, most times used for debug only. It can render simple shapes like circles, squares...
SpriteBatch
, used to render Texture
s Sprite
s...
So most times you will render using the SpriteBatch
. You render in a block made of:
SpriteBatch.begin()
, starts the SpriteBatch. Set his viewPort, Matrices... before this call
SpriteBatch.draw()
, draws the thing you give it as parameter (there are many different draw() methods, look at the API) to a Buffer, and flush()
them to the GPU as soon as it is full or SpriteBatch.end()
is called.
SpriteBatch.end()
, flush()
the buffer to the GPU, so everything is drawn on the screen.
Notes:
- Never have 2 SpriteBatches in their
begin
state at once. So before begin()
ing a SpriteBatch call end()
on the other running SpriteBatch
- If possible use only 1 SpriteBatch and try do call
end()
only once per render loop, as it cost some performance.
- Don't call
draw()
outside the begin-end block.
Last but not least for this "Tutorial":
The camera
. It helps you calculating your movements... in your own worldunits instead of pixels. Also you can use it to show different parts of your gameworld by moving it arround.
First: set your camerasviewport in the construcotr:
camera = new OrthographicCamera(16, 9);
This sets a viewport of 16 width and 9 height. So your physical screen/monitor is now 16 units width and 9 units heigh. To apply this camera to your SpriteBatch call:
`SpriteBatch.setProjectionMatrix(cam.combined)`
By doing this the SpriteBatch
renders the things your camera is looking at.
The cameras 0,0 point by deffault is in its middle. So to draw something in the middle now use:
batch.draw(yourTexture, 0, 0);
To draw in the upper right corner use:
batch.draw(yourTexture, 16 / 2, 9 / 2);
To move your camera use:
cam.translate(16/2, 9/2);
Make sure to call cam.update()
after you change its position!
Now the right upper corner of your camera is at 16,9 instead of 16/2, 9/2. And the 0,0 point is the left lower corner.
I hope this was not to complicated for the begining. Some tutorials, which helped me to learn:
- StarAssault
- Steigert's blog