catchbackkey不工作(libgdx)的Andr​​oid [英] catchbackkey not working (libgdx) Android

查看:232
本文介绍了catchbackkey不工作(libgdx)的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Android设备上的返回键setscreens我game..But它一点儿也不似乎工作..
我在网上看了几个帖子ñ这样做...

在menuscreen如果backkey是pressed应用程序应该exit..But没有行动被触发......即使我preSS返回键无数没有任何动作触发...

 公共类MenuScreen扩展AbstractScreen实现InputProcessor ​​{@覆盖
   公共无效显示()
   {
      Gdx.input.setInputProcessor(本);
      Gdx.input.setCatchBackKey(真);
      Gdx.input.setCatchMenuKey(真);}
   @覆盖
   公共布尔KEYDOWN(INT键code){
      // TODO自动生成方法存根
        如果(键code == Keys.BACK)
        {
             Gdx.app.exit();
          }      返回false;
   }

在LevelScreen当返回键是pressed的屏幕应该设置为menuscreen,但我的应用程序将退出
有没有办法禁止返回键??什么,如果它的pressed应该发生?

 公共类LevelScreen扩展AbstractScreen实现InputProcessor ​​{@覆盖
   公共无效显示()
   {
      Gdx.input.setInputProcessor(本);
      Gdx.input.setCatchBackKey(真);
      Gdx.input.setCatchMenuKey(真);}@覆盖
   公共布尔KEYDOWN(INT键code){
      如果(键code == Keys.BACK)
      {
                game.setScreen(game.geMenuScreen());
           }
      // TODO自动生成方法存根
      返回false;
   }


解决方案

首先,你可能想从输入处理器使用的keydown()来KEYUP()来改变。 KEYUP()将只被调用一次,当用户释放键,但KEYDOWN()将会被调用每一帧返回键下降(大约60次秒)。所以,你的应用程序可以切换画面,并在2帧关闭你这可能是不是你想要的。

为什么你看到从级别的屏幕应用程序关闭这是可能的。这将去LevelScreen - > ScoreScreen - > MenuScreen,然后在3帧(约50毫秒)狭路相逢。这里是一个开始在ScoreScreen应用程序的一些示例code。当用户presses(释放)后退按钮进入菜单画面。接下来,当他们preSS(和释放)再次后退按钮它,然后关闭应用程序。

您需要将此code适应你的游戏,但这应该给你的东西来看待和使用。 (注:我已经验证了在Android 2.3.3和0.9.8 LibGDX这个工作)

 公共类回测扩展游戏{
    公众屏幕菜单;
    公众屏幕分;    @覆盖公共无效创建(){
        Gdx.input.setCatchBackKey(真);
        this.menu =新MenuScreen(本);
        this.score =新ScoreScreen(本);
        this.setScreen(this.score);
    }    公共静态抽象类AbstractScreen实现屏幕{
        @覆盖公共无效渲染(最终浮动三角洲){}
        @覆盖公共无效调整大小(最终诠释宽度,最终诠释高度){}
        @覆盖公共无效显示(){}
        @覆盖公共无效的hide(){}
        @覆盖公共无效暂停(){}
        @覆盖公共无效简历(){}
        @覆盖公共无效的Dispose(){}
    }    公共静态类MenuScreen扩展AbstractScreen {
        公众最终回测游戏;        公共MenuScreen(最终回测游戏){
            this.game =游戏;
        }        @覆盖公共无效显示(){
            Gdx.app.log(菜单,展);
            Gdx.input.setInputProcessor(新InputAdapter(){
                @覆盖公共布尔KEYUP(最终诠释键code){
                    如果(键code == Keys.BACK){
                        Gdx.app.log(菜单,退出);
                        Gdx.app.exit();
                    }
                    返回false;
                }
            });
        }
    }    公共静态类ScoreScreen扩展AbstractScreen {
        公众最终回测游戏;        公共ScoreScreen(最终回测游戏){
            this.game =游戏;
        }        @覆盖公共无效显示(){
            Gdx.app.log(得分,展);
            Gdx.input.setInputProcessor(新InputAdapter(){
                @覆盖公共布尔KEYUP(最终诠释键code){
                    如果(键code == Keys.BACK){
                        Gdx.app.log(得分,后退);
                        ScoreScreen.this.game.setScreen(ScoreScreen.this.game.menu);
                    }
                    返回false;
                }
            });
        }
    }
}

I'm trying to use the back key on the android device to setscreens for my game..But it does'nt seem to work.. I read few posts online n did this...

In the menuscreen if the backkey is pressed the app should exit..But no action is triggered...Even if i press the back key numerous no action is triggered...

public class MenuScreen extends AbstractScreen implements InputProcessor  {

@Override
   public void show()
   {
      Gdx.input.setInputProcessor(this);
      Gdx.input.setCatchBackKey(true);
      Gdx.input.setCatchMenuKey(true);

}


   @Override
   public boolean keyDown(int keycode) {
      // TODO Auto-generated method stub
        if(keycode == Keys.BACK)
        { 
             Gdx.app.exit();
          }

      return false;
   }

In LevelScreen when the back key is pressed the screen should set to menuscreen , but my application will quit Is there way to disable the back key ??Nothing should happen if its pressed ?

  public class LevelScreen extends AbstractScreen  implements InputProcessor {

@Override
   public void show()
   {
      Gdx.input.setInputProcessor(this);
      Gdx.input.setCatchBackKey(true);
      Gdx.input.setCatchMenuKey(true);

}

@Override
   public boolean keyDown(int keycode) {
      if(keycode == Keys.BACK)
      {
                game.setScreen(game.geMenuScreen());
           }
      // TODO Auto-generated method stub
      return false;
   }

解决方案

Firstly, you probably want to change it from using keyDown() to keyUp() in the input processor. keyUp() will only be called once when the user releases the key, but keyDown() will be called every frame the back key was down (roughly 60 times a second). So your app could switch screens and close on you in 2 frames which likely isn't what you want.

This is likely why you see the app close from the level screen. It will be going LevelScreen -> ScoreScreen -> MenuScreen and then quitting in 3 frames (roughly 50 milliseconds). Here is some sample code of an application which starts in a ScoreScreen. When the user presses (and releases) the back button goes to the menu screen. Next, when they press (and release) the back button again it then closes the application.

You will need to adapt this code to your game, but this should give you something to look at and work with. (Note: I have verified this working on Android 2.3.3 and LibGDX 0.9.8)

public class BackTest extends Game {
    public Screen menu;
    public Screen score;

    @Override public void create() {
        Gdx.input.setCatchBackKey(true);
        this.menu = new MenuScreen(this);
        this.score = new ScoreScreen(this);
        this.setScreen(this.score);
    }

    public static abstract class AbstractScreen implements Screen {
        @Override public void render(final float delta) {}
        @Override public void resize(final int width, final int height) {}
        @Override public void show() {}
        @Override public void hide() {}
        @Override public void pause() {}
        @Override public void resume() {}
        @Override public void dispose() {}
    }

    public static class MenuScreen extends AbstractScreen {
        public final BackTest game;

        public MenuScreen(final BackTest game) {
            this.game = game;
        }

        @Override public void show() {
            Gdx.app.log("Menu", "Show");
            Gdx.input.setInputProcessor(new InputAdapter() {
                @Override public boolean keyUp(final int keycode) {
                    if (keycode == Keys.BACK) {
                        Gdx.app.log("Menu", "Quit");
                        Gdx.app.exit();
                    }
                    return false;
                }
            });
        }
    }

    public static class ScoreScreen extends AbstractScreen {
        public final BackTest game;

        public ScoreScreen(final BackTest game) {
            this.game = game;
        }

        @Override public void show() {
            Gdx.app.log("Score", "Show");
            Gdx.input.setInputProcessor(new InputAdapter() {
                @Override public boolean keyUp(final int keycode) {
                    if (keycode == Keys.BACK) {
                        Gdx.app.log("Score", "Back");
                        ScoreScreen.this.game.setScreen(ScoreScreen.this.game.menu);
                    }
                    return false;
                }
            });
        }
    }
}

这篇关于catchbackkey不工作(libgdx)的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆