Reputation: 737
I'm creating a game using LibGDX. Now I have two problems.
First, I'm trying to catch back key so that the game will pause. I'm already calling the Gdx.input.setCatchBackKey(true)
method in my Game
class. Here is the code :
public class CvSGame extends Game {
public Preferences prefs;
@Override
public void create() {
Gdx.input.setCatchBackKey(true);
prefs = Gdx.app.getPreferences("game_pref");
//setScreen(new SplashScreen(this));
//setScreen(new HomeScreen(this));
//setScreen(new GameScreen(this));
GamePlay.initialized(this);
}
}
GamePlay.initialized
is a method to set the Game
with a GameScreen
which implements Screen
and InputProcessor
.
In the GameScreen
, I already call setInputProcessor
. The code for GameScreen
is :
public class GameScreen implements Screen, InputProcessor{
CvSGame parent;
public GameScreen(CvSGame pParent){
parent = pParent;
Gdx.input.setInputProcessor(this);
}
@Override
public void show() {
}
@Override
public void resize(int width, int height) {
}
@Override
public void render(float delta) {
}
@Override
public void hide() {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
}
@Override
public boolean keyDown(int keycode) {
if(keycode == Keys.BACK) {
pauseGame();
}
return false;
}
@Override
public boolean keyUp(int keycode) {
return false;
}
@Override
public boolean keyTyped(char character) {
return false;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
return false;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return false;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
@Override
public boolean scrolled(int amount) {
return false;
}
private void pauseGame(){
GamePlay.gameState = GamePlay.PAUSED;
}
}
I think, if the Back button on my Android device is pressed, it will call the keyDown
method and the method pauseGame
will be called.
But, that's not happening. My game is exiting and the keyDown
method is not called (I'm already try to log a message if method keyDown is called, but the message never appear).
The second problem I have is caused by pausing game with the method pause()
. I think if the home button or the device receives a call, the method pause
in GameScreen
will be called. So, I think if I want to pause my game when the home button is pressed, I call the method pauseGame
in method pause
. And it works well. But the problem comes after I press back button so the game will quit. After the game quits and I try to start it again, my texture is not loaded.
By the way, currently I'm not using AssetManager
but call a method to load assets in the constructor.
Upvotes: 4
Views: 5967
Reputation: 548
You implement InputProcessor
on your class right? And also, as @MCeley said that you should put this code in order:
Gdx.input.setInputProcessor(this);
Gdx.input.setCatchBackKey(true);
Also, at the pauseMenu()
method, add a Boolean parameter, a good old flag like this:
private void pauseGame(boolean isPaused){
if(isPaused) {
GamePlay.gameState = GamePlay.PAUSED;
} else {
GamePlay.gameState = GamePlay.UNPAUSED;
}
}
Add the boolean value as flag and field in the GameScreen
class. Remember, flagging is like a marker and it's a better way to counter or reverse it. Before accepting my answer, you must study the flow on how the sequence works, starting at the end instead at the beginning. You must find out how it got there by re-trace the steps. Take note that this is a pseudocode, in a way.
Upvotes: 0
Reputation: 12745
It is my understanding that you need to set your InputProcessor
before calling setCatchBackKey
. You can fix this by changing your GameScreen
constructor to:
public GameScreen(CvSGame pParent) {
parent = pParent;
Gdx.input.setInputProcessor(this);
Gdx.input.setCatchBackKey(true);
}
Related question: In libgdx, how do I get input from the back button?
As for the texture not loading. Make sure that when your game is destroyed (dispose
function) that you're unloading all of your textures and disposing of them. When your game is recreated, be sure to load them all again.
All of the classes listed here need to be disposed of manually or they can cause leaks. In regards to textures, you can run into problems loading textures without first disposing of your older copies first.
Upvotes: 3
Reputation: 25177
For your use case (invoking a pause
method on back being pressed), you should not need to do anything special to catch the Back key. Libgdx will invoke the pause
method on your registered ApplicationListener
. See the ApplicationListener.pause()
javadoc which says:
Called when the Application is paused. An Application is paused before it is destroyed, when a user pressed the Home button on Android or an incoming call happened. On the desktop this will only be called immediately before dispose() is called.
The Game
ApplicationListener
will automatically forward pause
to the current Screen
, even.
Upvotes: 0