您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x编译安卓版本时实现“再按一次退出程序”的效果

2014-11-27 14:49 441 查看
我们常见的安卓项目都有再按一次退出程序的提示,或者是要有一个确认框,这样可以避免由于误按导致的程序的退出,所以,当我们通过cocos2d-x制作项目时,也常用到这样的功能,如果通过c++来实现的话,会相对麻烦些,况且不同地方都要设置,相对麻烦,而通过原生的java就可以很好的解决这个问题。

1、我用的cocos2d-x的版本为2.1.5,版本不同,可能具体的解决方法不同,但思路相同。

2、首先找到下图中的这个文件:src目录下的Cocos2dxGLSurfaceView.java文件



找到其中的onKeyDown函数,修改为如下代码:

@Override
publicboolean onKeyDown(finalint pKeyCode, finalKeyEvent pKeyEvent) {
switch(pKeyCode) {
caseKeyEvent.KEYCODE_BACK:
return false;
caseKeyEvent.KEYCODE_MENU:
this.queueEvent(newRunnable() {
@Override
publicvoid run() {
Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleKeyDown(pKeyCode);
}
});
returntrue;
default:
returnsuper.onKeyDown(pKeyCode, pKeyEvent);
}
}

?


也就是在监听到KEYCODE_BACK时,返回false,而之前返回的是true;
3、找到我们自己设置的安卓的包的标示文件,比如我的是com.planefight.org



打开其中的PlaneFight.java文件,重写onKeyDown方法。

系统自动生成的这个文件的原代码:

/****************************************************************************
Copyright (c) 2010-2011 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
packagecom.planefight.org;

importorg.cocos2dx.lib.Cocos2dxActivity;
importorg.cocos2dx.lib.Cocos2dxGLSurfaceView;

importandroid.os.Bundle;
importandroid.view.KeyEvent;
importandroid.widget.Toast;

publicclass PlaneFight extendsCocos2dxActivity{

protectedvoid onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}

publicCocos2dxGLSurfaceView onCreateView() {
Cocos2dxGLSurfaceView glSurfaceView = newCocos2dxGLSurfaceView(this);
// PlaneFight should create stencil buffer
glSurfaceView.setEGLConfigChooser(5,6,5,0,16,8);

returnglSurfaceView;
}

static{
System.loadLibrary("cocos2dcpp");
}
}

?



修改后的代码:

/****************************************************************************
Copyright (c) 2010-2011 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
packagecom.planefight.org;

importorg.cocos2dx.lib.Cocos2dxActivity;
importorg.cocos2dx.lib.Cocos2dxGLSurfaceView;

importandroid.os.Bundle;
importandroid.view.KeyEvent;
importandroid.widget.Toast;

publicclass PlaneFight extendsCocos2dxActivity{
privatelong exitTime = 0;
protectedvoid onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}

publicCocos2dxGLSurfaceView onCreateView() {
Cocos2dxGLSurfaceView glSurfaceView = newCocos2dxGLSurfaceView(this);
// PlaneFight should create stencil buffer
glSurfaceView.setEGLConfigChooser(5,6,5,0,16,8);

returnglSurfaceView;
}

@Override
publicboolean onKeyDown(intkeyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN)
{
if((System.currentTimeMillis()-exitTime) > 2000) //System.currentTimeMillis()无论何时调用,肯定大于2000
{
Toast.makeText(getApplicationContext(),"再按一次退出程序",Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
}
else
{
finish();
System.exit(0);
}

returntrue;
}
returnsuper.onKeyDown(keyCode, event);
}

static{
System.loadLibrary("cocos2dcpp");
}
}

也就是添加了重写的onKeyDown函数,并在之前声明了变量exitTime。

这样,就实现了在安卓应用任何页面按返回键均有提示“再按一次退出程序”;

文章转载于http://www.2cto.com/kf/201401/271518.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android cocos2d-x 游戏