接着昨天的返回键往下写,返回键的操作经常会应用到哪里呢?游戏退出时,下面是用jni实现的按返回键弹出原生对话框退出游戏的代码,有问题可以留言
.cpp
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) #include "platform/android/jni/JniHelper.h" #include "jni.h" #endif
void MenuLayer::onKeyReleased(EventKeyboard::KeyCode keyCode,Event* event) { #if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) if(keyCode == EventKeyboard::KeyCode::KEY_ESCAPE) { JniMethodInfo info; bool isExit = JniHelper::getStaticMethodInfo(info,"org/cocos2dx/cpp/AppActivity","exitGame","()V"); if(isExit) { CCLOG("hahahaha!!!"); info.env->CallStaticVoidMethod(info.classID,info.methodID); } else { CCLOG("on exist!!!"); } } #endif }
3.2版本的引擎pro.android项目里原来的XXActivity.java 入口类变成了AppActivity.java 看了看论坛里版主的解释是跟之前的没有什么区别,虽然是空的,但可以自己动手把onCreateonCreateView等补充完整,已紧不用在本类里处理加载lib了,下面直接贴出这个类的实现代码
public class AppActivity extends Cocos2dxActivity { public static Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case 1: Builder builder = new Builder(AppActivity.this); builder.setTitle("确认退出游戏?") .setPositiveButton("确定",new OnClickListener() { @Override public void onClick(DialogInterface dialog,int which) { System.exit(0); } }).setNegativeButton("取消",null).show(); break; default: break; } } }; } @Override public Cocos2dxGLSurfaceView onCreateView() { return super.onCreateView(); } public static void exitGame() { Message meg = new Message(); meg.what = 1; handler.sendMessage(meg); } }原文链接:https://www.f2er.com/cocos2dx/347085.html