问题描述
我使用LibGDX创造一个游戏。现在我有两个问题。
I'm creating a game using LibGDX. Now I have two problems.
首先,我试图捕捉返回键使游戏将暂停。我已经调用 Gdx.input.setCatchBackKey(真)
在我的游戏
类方法。这里是code:
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
是一个 GameScreen 设置
游戏
的方法code>它实现了屏幕
和 InputProcessor
。
在 GameScreen
,我已经叫 setInputProcessor
。为 GameScreen
的code是:
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;
}
}
我想,如果我的Android设备上的后退按钮是pressed,它会调用的keydown
方法,该方法 pauseGame
将被调用。
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.
但是,这没有发生。我的游戏正在退出和的keydown
方法不叫(我已经尝试登录的消息,如果方法的keydown被调用,但从来没有出现消息)。
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).
我的第二个问题是通过与方法暂停游戏引起暂停()
。我想,如果家里按钮或设备收到一个电话,该方法暂停
在 GameScreen
将被调用。所以,我觉得如果我想暂停我的游戏时,home键是pressed,我调用方法的方法 pauseGame
暂停
。而且效果很好。但是,问题就来了之后,我preSS后退按钮因此本场比赛将退出。游戏中退出,我尝试再次启动它后,我的质地没有加载。
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.
顺便说一句,我目前没有使用 AssetManager
,但调用一个方法来加载资产在构造函数中。
By the way, currently I'm not using AssetManager
but call a method to load assets in the constructor.
推荐答案
这是我的理解是,你需要设置你的 InputProcessor
之前调用 setCatchBackKey
。您可以通过改变你的 GameScreen
构造函数来解决这个问题:
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);
}
相关问题:In libgdx,我怎么从后面按键输入?
至于质地不加载。确保当你的游戏被销毁(处理
功能),你卸载所有的纹理,其中处置。当你的游戏被重建,一定要再次加载它们。
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.
这篇关于如何暂停,并使用返回键在LibGDX恢复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!