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

前端之家收集整理的这篇文章主要介绍了cocos2d-x编译安卓版本时实现“再按一次退出程序”的效果前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

转载自:http://blog.csdn.net/somestill/article/details/18046441


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

1、我用的cocos2d-x的版本为2.1.5,版本不同,可能具体的解决方法不同,但思路相同。(转载者注: 3.2版本亲测可用)

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

找到其中的onKeyDown函数如下:

[java] view plain copy
  1. @Override
  2. publicbooleanonKeyDown(finalintpKeyCode,finalKeyEventpKeyEvent){
  3. switch(pKeyCode){
  4. caseKeyEvent.KEYCODE_BACK:
  5. caseKeyEvent.KEYCODE_MENU:
  6. this.queueEvent(newRunnable(){
  7. voidrun(){
  8. Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleKeyDown(pKeyCode);
  9. }
  10. });
  11. returntrue;
  12. default:
  13. super.onKeyDown(pKeyCode,pKeyEvent);
  14. }
  15. }
修改为如下代码

  1. @Override
  2. finalKeyEventpKeyEvent){
  3. switch(pKeyCode){
  4. returnfalse;
  5. caseKeyEvent.KEYCODE_MENU:
  6. newRunnable(){
  7. @Override
  8. voidrun(){
  9. Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleKeyDown(pKeyCode);
  10. }
  11. });
  12. true;
  13. default:
  14. }
  15. }
也就是在监听到KEYCODE_BACK时,返回false,而之前返回的是true;
3、找到我们自己设置的安卓的包的标示文件,比如我的是com.planefight.org

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

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

    /****************************************************************************
  1. Copyright(c)2010-2011cocos2d-x.org
  2. http://www.cocos2d-x.org
  3. Permissionisherebygranted,freeofcharge,toanypersonobtainingacopy
  4. ofthissoftwareandassociateddocumentationfiles(the"Software"),todeal
  5. intheSoftwarewithoutrestriction,includingwithoutlimitationtherights
  6. touse,copy,modify,merge,publish,distribute,sublicense,and/orsell
  7. copiesoftheSoftware,andtopermitpersonstowhomtheSoftwareis
  8. furnishedtodoso,subjecttothefollowingconditions:
  9. Theabovecopyrightnoticeandthispermissionnoticeshallbeincludedin
  10. allcopiesorsubstantialportionsoftheSoftware.
  11. THESOFTWAREISPROVIDED"ASIS",WITHOUTWARRANTYOFANYKIND,EXPRESSOR
  12. IMPLIED,INCLUDINGBUTNOTLIMITEDTOTHEWARRANTIESOFMERCHANTABILITY,
  13. FITNESSFORAPARTICULARPURPOSEANDNONINFRINGEMENT.INNOEVENTSHALLTHE
  14. AUTHORSORCOPYRIGHTHOLDERSBELIABLEFORANYCLAIM,DAMAGESOROTHER
  15. LIABILITY,WHETHERINANACTIONOFCONTRACT,TORTOROTHERWISE,ARISINGFROM,
  16. OUTOFORINCONNECTIONWITHTHESOFTWAREORTHEUSEOROTHERDEALINGSIN
  17. THESOFTWARE.
  18. ****************************************************************************/
  19. packagecom.planefight.org;
  20. importorg.cocos2dx.lib.Cocos2dxActivity;
  21. importorg.cocos2dx.lib.Cocos2dxGLSurfaceView;
  22. importandroid.os.Bundle;
  23. importandroid.view.KeyEvent;
  24. importandroid.widget.Toast;
  25. classPlaneFightextendsCocos2dxActivity{
  26. protectedvoidonCreate(BundlesavedInstanceState){
  27. super.onCreate(savedInstanceState);
  28. publicCocos2dxGLSurfaceViewonCreateView(){
  29. Cocos2dxGLSurfaceViewglSurfaceView=newCocos2dxGLSurfaceView(this);
  30. //PlaneFightshouldcreatestencilbuffer
  31. glSurfaceView.setEGLConfigChooser(5,6,0); background-color:inherit">0,0); background-color:inherit">16,0); background-color:inherit">8);
  32. returnglSurfaceView;
  33. static{
  34. System.loadLibrary("cocos2dcpp");
  35. }
修改后的代码

  1. /****************************************************************************
  2. Copyright(c)2010-2011cocos2d-x.org
  3. http://www.cocos2d-x.org
  4. Theabovecopyrightnoticeandthispermissionnoticeshallbeincludedin
  5. allcopiesorsubstantialportionsoftheSoftware.
  6. FITNESSFORAPARTICULARPURPOSEANDNONINFRINGEMENT.INNOEVENTSHALLTHE
  7. OUTOFORINCONNECTIONWITHTHESOFTWAREORTHEUSEOROTHERDEALINGSIN
  8. THESOFTWARE.
  9. ****************************************************************************/
  10. packagecom.planefight.org;
  11. importorg.cocos2dx.lib.Cocos2dxActivity;
  12. importorg.cocos2dx.lib.Cocos2dxGLSurfaceView;
  13. importandroid.os.Bundle;
  14. importandroid.view.KeyEvent;
  15. importandroid.widget.Toast;
  16. @H_403_634@private@H_403_634@longexitTime=0;
  17. voidonCreate(BundlesavedInstanceState){
  18. super.onCreate(savedInstanceState);
  19. publicCocos2dxGLSurfaceViewonCreateView(){
  20. this);
  21. //PlaneFightshouldcreatestencilbuffer
  22. 8);
  23. returnglSurfaceView;
  24. @Override
  25. publicbooleanonKeyDown(intkeyCode,KeyEventevent)
  26. {
  27. if(keyCode==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_DOWN)
  28. if((System.currentTimeMillis()-exitTime)>2000)//System.currentTimeMillis()无论何时调用,肯定大于2000
  29. Toast.makeText(getApplicationContext(),"再按一次退出程序",Toast.LENGTH_SHORT).show();
  30. exitTime=System.currentTimeMillis();
  31. }
  32. else
  33. {
  34. finish();
  35. System.exit(0);
  36. }
  37. returntrue;
  38. super.onKeyDown(keyCode,event);
  39. static{
  40. System.loadLibrary("cocos2dcpp");
  41. 也就是添加了重写的onKeyDown函数,并在之前声明了变量exitTime。

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

    原文链接:https://www.f2er.com/cocos2dx/344947.html

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