I have a question regarding the pausing/freezing a game while a window/pausemenu will appear at center of the screen. I've already created a pausescreen class which will be called each time the back key is being pressed.
My problem is that I don't know how to freeze or put the game to pause, this becomes a problem because whenever i tried to click on the outsides of the pause window the player could move around.
superjumper code
follow this link . You will find a gameScreen , there they have specified game state which is responsible for pausing and resuming the game. Read it and your problem will be solved.
All you need is to check current game state in render() function. Here is sample solution: libgdx game how to display pause screen when user click on pause icon
Related
I want to make my program run in the background after you click a button.
I'm creating a rickroll (in 2015 yeh) and when you click a button I want to play the song never gonna give you up from rick astley and get the frame closed and run in a process, so the song still continues. I already got the part of clicking the button and starting the song, but how can I make it continue in a process?
I do not think that "continue in a process" is what you want.
Just close your frame, allow your main message loop to end, and before returning from your main() function block waiting for the sound to end. Or, if you don't know how to block waiting for the sound to end, then calculate how many milliseconds long your sound is, and just Thread.sleep() for that many milliseconds.
I programmed a game made of multiple screens and every time I need a new screen I use the code
dispose();
game.setScreen(new GameScreen(game));
Inside the dispose method I called the dispose method of each asset like Textures and so on.
I also use the asset manager to load the assets required for the game play screen. In that case when I close the game play screen I also call AssetManager.clear().
Now when I start the game I only have the menu screen and the used memory is about 20MB, then in the game play screen I reach 212MB and when I come back to the menu screen, after disposing the AssetManger, I still have 186MB.
The main problem is that if I start a new game play screen the memory reaches 320MB so after some game play screens the game reaches 700MB!
What's wrong with my code?
You should never have to manually call your Game class's dispose() method. It's called automatically upon changing screens or closing the app. Instead, you should override the Game dispose() like this (code taken from a project I'm working on):
#Override
public void dispose()
{
super.dispose();
assets.dispose();
}
The assets object is my AssetManager. Upon closing the app, I want all my loaded textures to be destroyed from memory. I can't promise anything, but I'm pretty sure that's your problem.
I have a game in which if user touches sound button and that should toggle sound on and off but with every touch many touches are registered. For this reason sound is sometimes off and sometime on.
You are probably using Gdx.input.isTouched();
The problem is that you are using that in your update or render method, and you keep your finger on the screen for more than 1 frame, and the audio changes very fast.
In your show method (or create) use
Gdx.input.setInputProcessor(new InputProcessor(...) );
And in the TouchDown method add your code, because that function is called just once whenever the touch is down, not every frame you are touching your screen.
In my LibGDX game for android, if the user backs out of the game (either by pressing the home button or switching to another application) LibGDX's built in pause() method is supposed to run. Now, this is fine, and it works fine as well. My problem is that if I back out of the game to do whatever, and then rejoin the game, it has restarted the app completely (kind of like if every time you exited and rejoined in the middle of a game of Pacman your score would be zero and all the dots would be back). For my screen switching, it is necessary that the game NOT restart every time the user exits, but simply enter the corresponding state to actually simulate the 'paused' game. How do I stop LibGDX/Android from killing the game altogether upon user exit, but simply pausing it?
The libGDX application lifecycle matches the lifecycle of the Android Activity as documented in the ApplicationListener interface so you should expect the same behavior. When you press the home button while in a libGDX game then the pause method will be called, which is the same as onPause in Android. The game will go to the background but will stay in memory. However this is not guaranteed and the OS might release the games memory for other applications, there really is no way to get around this. In the case when the game comes back to the foreground and the game restarts you'll need to load the games state from when it was paused.
I've written my own article on how to save and load the game state using Json in libGDX, maybe that will be useful to you.
You should use Asset Manager to prevent it, here a good tutorial :
http://code.google.com/p/libgdx/wiki/AssetManager
the official doc :
http://code.google.com/p/libgdx/wiki/AssetManager
Load all of your textures with assets manager
I have the same problem, but I found the solution by:
Disable option in Developer options called
- Don't keep activities (Destroy every activity as soon as the user leaves it)
Well I've been developing a 2D java game for a while but I face with a strange problem and I'm stuck :/. So that I'm uploading my source code here.
It is an eclipse project, in this project I have included 2 main methods to clearly state the problem i have experienced:
In Menu class, starts the system from the menu and menu passes the frame to the game manager after pressing play game button
In GameManager class, starts the system by passing a frame to game manager directly.
My problem is that: when I start the system from Menu class and press play game button, rendering continues but system does not respond to any actions even if I had added listeners to canvas, but it works when I start the system from the main method which belongs to GameManager class. :/
Here is my source code: http://www.mediafire.com/?4d2v1jfizrgpaqp
Thanks