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.