天天看點

cocos2d-x編譯安卓版本時實作“再按一次退出程式”的效果

        我們常見的安卓項目都有再按一次退出程式的提示,或者是要有一個确認框,這樣可以避免由于誤按導緻的程式的退出,是以,當我們通過cocos2d-x制作項目時,也常用到這樣的功能,如果通過c++來實作的話,會相對麻煩些,況且不同地方都要設定,相對麻煩,而通過原生的java就可以很好的解決這個問題。

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

        2、首先找到下圖中的這個檔案:src目錄下的Cocos2dxGLSurfaceView.java檔案

cocos2d-x編譯安卓版本時實作“再按一次退出程式”的效果

找到其中的onKeyDown函數如下:

@Override
	public boolean onKeyDown(final int pKeyCode, final KeyEvent pKeyEvent) {
		switch (pKeyCode) {
			case KeyEvent.KEYCODE_BACK:
			case KeyEvent.KEYCODE_MENU:
				this.queueEvent(new Runnable() {
					@Override
					public void run() {
						Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleKeyDown(pKeyCode);
					}
				});
				return true;
			default:
				return super.onKeyDown(pKeyCode, pKeyEvent);
		}
	}
           

修改為如下代碼:

@Override
	public boolean onKeyDown(final int pKeyCode, final KeyEvent pKeyEvent) {
		switch (pKeyCode) {
			case KeyEvent.KEYCODE_BACK:
				return false;
			case KeyEvent.KEYCODE_MENU:
				this.queueEvent(new Runnable() {
					@Override
					public void run() {
						Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleKeyDown(pKeyCode);
					}
				});
				return true;
			default:
				return super.onKeyDown(pKeyCode, pKeyEvent);
		}
	}
           

也就是在監聽到KEYCODE_BACK時,傳回false,而之前傳回的是true;

       3、找到我們自己設定的安卓的包的标示檔案,比如我的是com.planefight.org

cocos2d-x編譯安卓版本時實作“再按一次退出程式”的效果

打開其中的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.
****************************************************************************/
package com.planefight.org;

import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;

import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;

public class PlaneFight extends Cocos2dxActivity{
	
    protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);	
	}

    public Cocos2dxGLSurfaceView onCreateView() {
    	Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
    	// PlaneFight should create stencil buffer
    	glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);
    	
    	return glSurfaceView;
    }
    

    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.
****************************************************************************/
package com.planefight.org;

import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;

import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;

public class PlaneFight extends Cocos2dxActivity{
	private long exitTime = 0;
    protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);	
	}

    public Cocos2dxGLSurfaceView onCreateView() {
    	Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
    	// PlaneFight should create stencil buffer
    	glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);
    	
    	return glSurfaceView;
    }
    
    
    @Override
    public boolean onKeyDown(int keyCode, 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);
			     }
			             
			     return true;
	     }
	     return super.onKeyDown(keyCode, event);
    }
    

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

也就是添加了重寫的onKeyDown函數,并在之前聲明了變量exitTime。

這樣,就實作了在安卓應用任何頁面按傳回鍵均有提示“再按一次退出程式”;

本文原創,轉載請注明 出處。

繼續閱讀