How to close a game that uses multiple Screen in LIbgdx - java

I have a class that extends Game, then a bunch of other classes that implements Screen and because of that I can just do this game.setScreen(new MyScreen(game)); but the problem is that when I do Gdx.app.exit() it just closes the current screen and go to the next one, this is the same for game.dispose().So how do you close or exit the game when it's running as desktop or android application?

You could try doing System.exit(0), which should work.
I believe (although I haven't tested this one out), you can also add the line: config.forceExit(false) whereas it was previously set to true.

Related

libGDX: is it better to re-use Screens or create new instances every time?

Here's my case. I have three different types of screens:
MainMenuScreen
Has a "Start" button for switching to the GameScreen. Also lets player change basic game settings.
GameScreen
The actual playable game (where the player character runs and jumps).
GameOverScreen
Screen that plays an animation and then displays a menu that lets the player start the game again (i.e. switch to GameScreen) or return the main menu (i.e. switch to MainMenuScreen).
Am I better off storing my Screens in variables and reusing them when I switch screens, or is it better to dispose of each Screen when I'm done with it and then create a new instance of the screen type I intend to switch to?
And would the answer be different in a case where Screen switching is more frequent (like between an overworld Screen and a battle Screen in a game like Final Fantasy or Pokemon)?
Thanks!
In my opinion you should reuse Screens, which are used frequently and dispose and recreate Screens not used that often.
In your example the GameScreen, GameOverScreen and MainMenuScreen should all be reused, as they are directly connected to each other:
As soon as a player starts a game, it is possible, that he dies. Then the GameOverScreen is shown and directly after that the MainMenuScreen is shown. So the user does not need to switch to those Screens manually.
The OptionsScreen instead, could be disposed and recreated every time, because usualy you open the OptionScreen only a few times. Also it is not directly connected to any other Screen, but the user needs to "force" the game to open the OptionScreen.
But as long as there aren't too many Screens, reusing shouldn't be a problem.
In your second example, the Screens are used even more often, so they definitly should be reused.
EDIT:
As #EssEllDee mentioned, you should also make use of hide() and show(). Those methods get called by Libgdx, when you switch the Screen and they can be used to dispose/recreate heavy ressources. For example, you might dispose Textures used for the game, when you switch to the GameOverScreen and reload them, when switching to the GameScreen.

Gdx exit closing entire application - how to close only the gdx frame?

I have a game editor, in which it is possible to launch the game being edited in a separate window (but within the same VM). However, when the game is closed, I would like to close its gdx window, without bringing down the whole application (that is, the editor).
I am currently using the following code inside the JFrame that hosts the LwjglApplication:
public void windowClosing(WindowEvent e) {
System.err.println("Now closing app...");
Gdx.app.exit();
System.err.println("App now closed.");
}
This prints the goodbye, closes the GDX window, and proceeds to terminate my VM. Any suggestions?
In the desktop (lwjgl backend) Gdx.app.exit() posts a Runnable that causes the loop mainLoop to complete and control to fall out the bottom of that function (see the source). The mainLoop ends with:
if (graphics.config.forceExit) System.exit(-1);
The graphics.config is the LwjglApplicationConfiguration object passed in LwjglApplication constructor. So just set
config.exit = false
in the config object (you may need to create one and use a different constructor if you're not currently creating a config object). There are other handy things you can set in the config object, too.
(This code is from GIT, so things may be different in older version of GDX or with other backends.)

is multiple activities and surface views the correct way to go?

I'm currently in the process of making one of my first android games and have come into some difficulty understanding how to make the transitions between screens.. for example:
My game starts its main activity, which then loads TitleScreen surface view which initializes its own thread
on tap I start a new intent which loads a new activity which loads GameView surface view which initializes its own thread
This all works fine when testing on my device (Evo 3d) but crashes on tap on my test bed, I'm using android x86 in virtual box for quick testing. Is this likely to be a problem in my code or a problem with the simulator?
Also I'm wanting to add a level select screen in between the title screen and the game screen and figured i could do this by creating another activity/surface view/thread combo, Is this acceptable coding practice or is this a wasteful/process heavy method?
You could create a variety of methods that you call from your onDraw method. Each method would draw one screen (game, level, score). To start simple a switch case in the onDraw checks the screen and then calls the right thing to draw.
If you want to have different layers, you should use different acitvities so that the background (game) is being paused while the scoreboard is active. This only makes sense if you want the background to be still visible or need the acitivites for other reasons.
But you should never have more than one surface view active at the same time, android doesnt like that.
I think its not good to use more activities for single application. Try to use ViewFlipper with number of xml layout files. Here you can apply transition effects very easily.
I am suggesting you it for transition effects, but you also check it once. I am also thinking which one is good.

Android, where to put my UI updates and my game loop

So I'm new to Android, and have been having some difficulties with understanding threading/Android UI updating and such. I have some code for a simple game engine I made for an AP computer science final project and I have been trying to make it into an Android app. Coming from the java world I'm not used to threading or worrying about how much time or where a calculation is taking place so I've been having some difficulties getting my game playable. After a splash screen and a main menu, I have it set up so an activity named "Play" starts. In this activity I have found that I can initialize my game engine object(which gets passed from class to class), create an object I created to make an AsyncTask("GuiThreader" in the code linked below) but as soon as I throw in some code in "Play" to do anything more than that (like initialize a button, or start the threader helper class) I get an "Activity has Stopped Unexpectedly" error meaning I'm doing something wrong. I've been looking at a lot of the Android example code but its making little sense to me. So I guess with all that background my bigger question is how can I get this code working? More specifically where should I have my loop that checks when the game is over, and how can I update my button colors outside of the "main" threader to keep it from crashing.
Here is my code:
"Play" Activity (with lines of code commented that I was testing with):
http://pastebin.com/K5kFsMvG
"GuiThreader" used to make the game calculations an AsyncTask: http://pastebin.com/306eUYfq
"GUIdriver" used to call a class that updates the button colors: http://pastebin.com/RANZBH38
"ButtonColorUpdate" saves a value for buttons and updates their colors: http://pastebin.com/qN2fw1RC
If you need anything else just comment and I'll put it up. Thanks in advance for any help!
Start by reading this. Then I'd recommend looking at the code from Replica Island. There's a ton you can learn from studying the code of this project. If you want to interact with GUI "stuff" from a separate thread, a Handler (see the API) can be useful for sending messages between the two.

Blackberry java app running in background

I would like to exit the application with 2 different ways:
- When key "escape" pressed, the application exit but still run in background.
- When the user select "Close" in the menu to close the application totally. This is already working as it is the default behavior.
In my MainScreen class, I have overwritten the close() method that call super.close() at the end in order to close the screen.
Thank you
See the method Application.requestBackground()
http://www.blackberry.com/developers/docs/4.5.0api/net/rim/device/api/system/Application.html#requestBackground()

Categories

Resources