cocos2d-x 3.2 实现 Andorid “请再按一次退出游戏”,双击返回键退出游戏

前端之家收集整理的这篇文章主要介绍了cocos2d-x 3.2 实现 Andorid “请再按一次退出游戏”,双击返回键退出游戏前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

首先将cocos2d/cocos/platform/android/src/org/cocos2dx/lib下的Cocos2dxGLSurfaceView.java里的

public boolean onKeyDown(final int pKeyCode,final KeyEvent pKeyEvent) 函数修改一下,改成如下样式,让KeyEvent.KEYCODE_BACK时直接返回false,防止其阻断我们对返回键的监听。

    public boolean onKeyDown(final int pKeyCode,final KeyEvent pKeyEvent) {
        switch (pKeyCode) {
            case KeyEvent.KEYCODE_BACK:
            	return false;
                // Cocos2dxVideoHelper.mVideoHandler.sendEmptyMessage(Cocos2dxVideoHelper.KeyEventBack);
            case KeyEvent.KEYCODE_MENU:
            case KeyEvent.KEYCODE_DPAD_LEFT:
            case KeyEvent.KEYCODE_DPAD_RIGHT:
            case KeyEvent.KEYCODE_DPAD_UP:
            case KeyEvent.KEYCODE_DPAD_DOWN:
            case KeyEvent.KEYCODE_ENTER:
            case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
            case KeyEvent.KEYCODE_DPAD_CENTER:
                this.queueEvent(new Runnable() {
                    @Override
                    public void run() {
                        Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleKeyDown(pKeyCode);
                    }
                });
                return true;
            default:
                return super.onKeyDown(pKeyCode,pKeyEvent);
        }
    }


之后在跟根目录的src/org.cocos2dx.cpp/AppActivity.java里面的主函数添加如下代码即可


private long mkeyTime = 0;
public boolean onKeyDown(int keyCode,KeyEvent event) {
    //二次返回退出
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        if ((System.currentTimeMillis() - mkeyTime) > 2000) {
            mkeyTime = System.currentTimeMillis();
            Toast.makeText(this,"请再按一次退出游戏",Toast.LENGTH_LONG).show();
        } else {
            finish();
            System.exit(0);
        }
        return false;
    }
    return super.onKeyDown(keyCode,event);
}
原文链接:https://www.f2er.com/cocos2dx/344671.html

猜你在找的Cocos2d-x相关文章