When I press back button on android, it exits the game although I ask it to go to other screen instead. It goes to the other screen for one or two seconds then it exits.:
if((Gdx.input.isKeyPressed(Input.Keys.BACK)))
game.setScreen(new GameOverScreen(game,4));
if(controller.isresumePressed())
{
controller.table2visible(false);
controller.table1visible(true);
pausestate = false;
}
You also need to tell LibGDX to catch the back key from Android. This only needs to be done once so you can do this in the onCreate() method of your main class.
Gdx.input.setCatchBackKey(true);
Related
I have an application that contains many game screens, each is its own activity. Some of these game screens will use timers to initiate some functionality. The problem I am having is when the user backs out of one of these game screens, the timers are still running in the background. I know this because when I leave to the main menu the timer loads up the losing screen because the timer and activity were still running in the background process. I simply want to kill the activity if the user hits the phone back button (Not a button I have created but the devices back button).
This is the current method I use to try and accomplish this:
#Override
public void onBackPressed() {
super.onBackPressed();
this.finish();
}
It does not kill the activity.
EDIT:
I Should mention that stopping the timers does work but for some of my screens the win or lose conditions are done when a variable hits a certain number. For example, I have one screen that when an enemy escapes the view of the player a variable goes up by 1 when this hits 5 the player loses. This is not done on a timer, so because the activity is still running, the variable hits 5 and the losing screen is loaded when I am not on that game screen anymore.
Only the visible Activity will receive that finish() command. You need to start and stop your timers onResume and onPause
First of all, remove
super.onBackPressed();
And add
yourActivity.this.finish();
Let me know if that helps :)
I got two activities in my game application. One parent menu activity with two buttons, each having a onClick, and one child (game) activity containing the game loop.
When I press the back button from the game activity, the menu seems to be properly resumed: Both buttons are at the right position and look like before.
The problem is that the buttons don't work anymore. Same goes for the android's back button. Although the back button flashes up after clicking on it, nothing happens.
My guess is that it has something to do with android's memory management or the game loop. I destroy the game thread inside of my SurfaceView's surfaceDestroyed:
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
while (retry) {
try {
gameEngine.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
I use no custom overriden methods for resuming and restarting. Since the activity's appearance resumes finely and the onResume() method is called on resuming to the menu activity, I see no way to debug into it deeper.
The problem was that the running thread in the child activity was not correctly stopped - I was missing a setRunning(false) before joining. I assume that the UI Threads did their work anyway. After fixing the thread, the back button works properly.
I'm using LIBGDX for my gamedev. I'm using com.badglogic.gdx.audio.Sound for my game sounds. I have a weird problem in my game screen . If I press the HOME button of my phone to exit the game and I Sound.stop the sound in the hide event of the game screen then when I re-enter the game I don't hear the sound anymore and that is what is supposed to happen. But if I instead use the Sound.pause method (instead of stop) then I hear the sound when I re-enter the game instance.
Why does Sound.stop work and Sound.pause doesn't?
PS: I've removed all the Sound.resume method instances from my code that would possible create such behavior.
works:
#Override
public void hide(){
spaceship_sound.stop(id_spaceship_sound);
}
doesn't work:
#Override
public void hide(){
spaceship_sound.pause(id_spaceship_sound);
}
I've even put the pause method in the keyDown (HOME button) and pause game screen events, practically all over the code with no luck.
I create a dialog that is a pause menu when a player pauses the game/leaves the app and when you return and for example resume, it doesn't play the exit animation that it is supposed to, it only happens when you leave the app and return to it. Is there a way to fix it? Is it normal?
Because its not the animation fault, which is working perfectly fine when you pause/resume in app.
I went around it by dismissing the dialog on app exit and showing another one on return.
My app plays a coin sound every time a button is pressed.
coin_sound.start();
You can easily press faster than the coin sound. When this happens I want the coin sound to start from the beginning ever time the button is pressed.
if(coin_sound.isPlaying()){
coin_sound.reset();
coin_sound = MediaPlayer.create(getContext(), R.raw.coin02);
}
coin_sound.start();
The problem with this is that loading a media file tiny as it may be is still a relatively slow process. When you start to click the button really fast the app lags hard.
Are there any solutions to my problem? The only idea I have is to do something with an array of coin_sounds, but this method seems like it will be messy and gross...
The other answer posted here is somewhat correct. You should not call create over and over.
The code in that answer has a problem, though. The reset method sends the MediaPlayer into the idle state, where it is illegal to call most other methods. If you were to go that route, you have to call methods in the following order:
coin_sound.reset();
coin_sound.setDataSource(...);
coin_sound.prepare();
coin_sound.start();
The difference between calling create and the previous sequence of method calls is simply the creation of a new instance. That, however, is not the quickest way to do what should be done.
You should simply call coin_sound.seekTo(0); when you want the current playing sound to restart. So do something like:
if (coin_sound.isPlaying()) coin_sound.seekTo(0);
else coin_sound.start();
That assumes you have left the MediaPlayer in the prepared state so start can be called. You can accomplish that by calling reset, setDataSource, and prepare in the onCompletion listener. Also, make sure to call release when the sound is no longer needed.
It is because you are initiating coin_sound in the button click event, try this
initiate this variable in your oncreate method
coin_sound = MediaPlayer.create(getContext(), R.raw.coin02);
then make this your code for your button
if(coin_sound.isPlaying()){
coin_sound.reset();
}
coin_sound.start();
the problem is you are recreating a new media player each time the button is clicked so the new media player doesnt think there is a sound
and do you need to start it again with coin_sound.start();? doesnt restart stop then start the sound for you?