fbjorn
fbjorn

Reputation: 890

Composing the game screen (LibGDX)

Seems like I'm confused with the next stuff:

I was trying hard but still can't undestand the difference of these terms. What I'm trying to do: Create a Class which will be extended from Game (implements ApplicationListener or Screen), it will be the area, where all actions happen (player movement, spawning enemies, etc). Then create a class that will be probably implemented the Screen. Score, multipliers and some usefull text will be there. Then I want to create a stage in another class that includes my classes (thin line with score on high of the screen and the area with the game below. In other words I want to divide the game and text).

Hope you get the idea, so how should I realize the classes with the game, score and a main class which includes them? (Screen, ApplicationListener, Game) Or there is a way how to do it easier?

Upvotes: 0

Views: 1047

Answers (2)

noone
noone

Reputation: 19796

The ApplicationListener is the entry point of your LibGDX application. LibGDX has several backends to support multiple platforms. All those backends get an ApplicationListener which will be one of your self-made classes.

Game actually implements ApplicationListener which means that you could also supply a Game instead of an ApplicationListener. But a Game has more functionality, since it is a class and not only an interface. It provides you with a basic approach to split your game in several logical parts, which will keep your code more clean. Those parts are called Screens. A Game will always have one Screen which will count as the active one and all the methods from Game are actually just being forwarded to the currently active Screen.

A Screen is one more of your own classes which you implement. It could be a SplashScreen, a LoadingScreen or a GameplayScreen... maybe a MainMenuScreen and an OptionsScreen. What about a HighscoresScreen? You get the idea here. Whenever you want to switch the Screen you will use Game.setScreen() to do so. This in turn will call Screen.hide() on the current Screen and Screen.show() on the next Screen.

Upvotes: 3

Robert P
Robert P

Reputation: 9823

The ApplicationListener Interface gives you all the Methods, which are called by the main game loop:

  1. create() is called when your App is started for the first time.
  2. dispose() is called when your App is getting closed.
  3. pause() is called when a call is incoming or you press the homebutton.
  4. resume() is called, when you come back to the App, after pause() has been called.
  5. resize() is called on desktop when you resize the window.
  6. render() is called once every Gameloop (max 60 times per ).

Extending Game does more or less the same, but it allready has some deffault implementations. Those deffault implementations call this functions on the current screen. So if you start your App create() is called on your Game class and this calls create() for your current screen.

A Screen reppresents the things which have to be rendered on Screen. Most times there are different Screens for different logics. So for example a MainMenuScreen a GameScreen and a OptionsScreen. Those Screens can be set by calling setScreen() in your Game class. This automatically calls hide() for the current Screen and show() for the new Screen. I hope this helps you by understanding Libgdx.

Upvotes: 1

Related Questions