对于cocos2dx在android平台事件的响应过程很模糊,于是分析了下源码,cocos2dx 版本3.4,先导入一个android工程,然后看下AndroidManifest.xml

  1. <application android:label="@string/app_name"
  2. android:icon="@drawable/icon">
  3.  
  4. <!-- Tell Cocos2dxActivity the name of our .so -->
  5. <meta-data android:name="android.app.lib_name"
  6. android:value="cocos2dcpp" />
  7.  
  8. <activity android:name="org.cocos2dx.cpp.AppActivity"
  9. android:label="@string/app_name"
  10. android:screenOrientation="landscape"
  11. android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
  12. android:configChanges="orientation">
  13.  
  14. <intent-filter>
  15. <action android:name="android.intent.action.MAIN" />
  16. <category android:name="android.intent.category.LAUNCHER" />
  17. </intent-filter>
  18. </activity>
  19. </application>

由此得知启动窗口类为 org.cocos2dx.cpp.AppActivity,并继承之 Cocos2dxActivity

  1. package org.cocos2dx.cpp;
  2.  
  3. import org.cocos2dx.lib.Cocos2dxActivity;
  4.  
  5. public class AppActivity extends Cocos2dxActivity {
  6. }
  1. public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelperListener

看下 Cocos2dxActivity 的 onCreate

  1. @Override
  2. protected void onCreate(final Bundle savedInstanceState) {
  3. Log.i(TAG, "------onCreate----");
  4. super.onCreate(savedInstanceState);
  5. CocosPlayClient.init(this, false);
  6.  
  7. onLoadNativeLibraries();//加载了一些静态库
  8.  
  9. sContext = this;
  10. this.mHandler = new Cocos2dxHandler(this);
  11.  
  12. Cocos2dxHelper.init(this);
  13.  
  14. this.mGLContextAttrs = getGLContextAttrs();
  15. this.init();//初始化
  16.  
  17. if (mVideoHelper == null) {
  18. mVideoHelper = new Cocos2dxVideoHelper(this, mFrameLayout);
  19. }
  20.  
  21. if(mWebViewHelper == null){
  22. mWebViewHelper = new Cocos2dxWebViewHelper(mFrameLayout);
  23. }
  24. }

onCreate 调用了  init()  初始化

  1. public void init() {
  2.  
  3. // FrameLayout
  4. ViewGroup.LayoutParams framelayout_params =
  5. new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
  6. ViewGroup.LayoutParams.MATCH_PARENT);
  7. mFrameLayout = new FrameLayout(this);
  8. mFrameLayout.setLayoutParams(framelayout_params);
  9.  
  10. // Cocos2dxEditText layout
  11. ViewGroup.LayoutParams edittext_layout_params =
  12. new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
  13. ViewGroup.LayoutParams.WRAP_CONTENT);
  14. Cocos2dxEditText edittext = new Cocos2dxEditText(this);
  15. edittext.setLayoutParams(edittext_layout_params);
  16.  
  17. // ...add to FrameLayout
  18. mFrameLayout.addView(edittext);
  19.  
  20. // Cocos2dxGLSurfaceView
  21. this.mGLSurfaceView = this.onCreateView();
  22.  
  23. // ...add to FrameLayout
  24. mFrameLayout.addView(this.mGLSurfaceView);
  25.  
  26. // Switch to supported OpenGL (ARGB888) mode on emulator
  27. if (isAndroidEmulator())
  28. this.mGLSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
  29.  
  30. this.mGLSurfaceView.setCocos2dxRenderer(new Cocos2dxRenderer());
  31. this.mGLSurfaceView.setCocos2dxEditText(edittext);
  32.  
  33. // Set framelayout as the content view
  34. setContentView(mFrameLayout);
  35. }

最终显示的视图为 this.mGLSurfaceView = this.onCreateView();

  1. public Cocos2dxGLSurfaceView onCreateView() {
  2. Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
  3. //this line is need on some device if we specify an alpha bits
  4. if(this.mGLContextAttrs[3] > 0) glSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
  5.  
  6. Cocos2dxEGLConfigChooser chooser = new Cocos2dxEGLConfigChooser(this.mGLContextAttrs);
  7. glSurfaceView.setEGLConfigChooser(chooser);
  8.  
  9. return glSurfaceView;
  10. }

Cocos2dxGLSurfaceView 就是最终显示的视图,事件处理也在这个类中,包括 onResume,onPause,onSizeChanged,onKeyDown,onTouchEvent等 主要看下onTouchEvent事件的处理过程

  1. @Override
  2. public boolean onTouchEvent(final MotionEvent pMotionEvent) {
  3.  
  4. //Log.d(TAG, "------onTouchEvent action=----"+pMotionEvent.getAction());
  5.  
  6. // these data are used in ACTION_MOVE and ACTION_CANCEL
  7. final int pointerNumber = pMotionEvent.getPointerCount();
  8. final int[] ids = new int[pointerNumber];
  9. final float[] xs = new float[pointerNumber];
  10. final float[] ys = new float[pointerNumber];
  11.  
  12. for (int i = 0; i < pointerNumber; i++) {
  13. ids[i] = pMotionEvent.getPointerId(i);
  14. xs[i] = pMotionEvent.getX(i);
  15. ys[i] = pMotionEvent.getY(i);
  16. }
  17.  
  18. switch (pMotionEvent.getAction() & MotionEvent.ACTION_MASK) {
  19. case MotionEvent.ACTION_POINTER_DOWN:
  20. final int indexPointerDown = pMotionEvent.getAction() >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
  21. final int idPointerDown = pMotionEvent.getPointerId(indexPointerDown);
  22. final float xPointerDown = pMotionEvent.getX(indexPointerDown);
  23. final float yPointerDown = pMotionEvent.getY(indexPointerDown);
  24.  
  25. this.queueEvent(new Runnable() {
  26. @Override
  27. public void run() {
  28. Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleActionDown(idPointerDown, xPointerDown, yPointerDown);
  29. }
  30. });
  31. break;
  32.  
  33. case MotionEvent.ACTION_DOWN:
  34. // there are only one finger on the screen
  35. final int idDown = pMotionEvent.getPointerId(0);
  36. final float xDown = xs[0];
  37. final float yDown = ys[0];
  38.  
  39. this.queueEvent(new Runnable() {
  40. @Override
  41. public void run() {
  42. Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleActionDown(idDown, xDown, yDown);
  43. }
  44. });
  45. break;
  46.  
  47. case MotionEvent.ACTION_MOVE:
  48. this.queueEvent(new Runnable() {
  49. @Override
  50. public void run() {
  51. Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleActionMove(ids, xs, ys);
  52. }
  53. });
  54. break;
  55.  
  56. case MotionEvent.ACTION_POINTER_UP:
  57. final int indexPointUp = pMotionEvent.getAction() >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
  58. final int idPointerUp = pMotionEvent.getPointerId(indexPointUp);
  59. final float xPointerUp = pMotionEvent.getX(indexPointUp);
  60. final float yPointerUp = pMotionEvent.getY(indexPointUp);
  61.  
  62. this.queueEvent(new Runnable() {
  63. @Override
  64. public void run() {
  65. Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleActionUp(idPointerUp, xPointerUp, yPointerUp);
  66. }
  67. });
  68. break;
  69.  
  70. case MotionEvent.ACTION_UP:
  71. // there are only one finger on the screen
  72. final int idUp = pMotionEvent.getPointerId(0);
  73. final float xUp = xs[0];
  74. final float yUp = ys[0];
  75.  
  76. this.queueEvent(new Runnable() {
  77. @Override
  78. public void run() {
  79. Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleActionUp(idUp, xUp, yUp);
  80. }
  81. });
  82. break;
  83.  
  84. case MotionEvent.ACTION_CANCEL:
  85. this.queueEvent(new Runnable() {
  86. @Override
  87. public void run() {
  88. Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleActionCancel(ids, xs, ys);
  89. }
  90. });
  91. break;
  92. }
  93.  
  94. /*
  95. if (BuildConfig.DEBUG) {
  96. Cocos2dxGLSurfaceView.dumpMotionEvent(pMotionEvent);
  97. }
  98. */
  99. return true;
  100. }

看下 MotionEvent.ACTION_DOWN,调用了Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleActionDown

  1. public void handleActionDown(final int id, final float x, final float y) {
  2. Log.i("Cocos2dxRenderer","-----handleActionDown--");
  3. Cocos2dxRenderer.nativeTouchesBegin(id, x, y);
  4. }

这里的nativeTouchesBegin 是一个jni方法,是现在cocos2d\cocos\platform\android\jni\TouchesJni.cpp里,

  1. private static native void nativeTouchesBegin(final int id, final float x, final float y);
  1. JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesBegin(JNIEnv * env, jobject thiz, jint id, jfloat x, jfloat y) {
  2. intptr_t idlong = id;
  3. log("----Info:nativeTouchesBegin id = %d, x=%f, y=%f",id,x,y);
  4. cocos2d::Director::getInstance()->getOpenGLView()->handleTouchesBegin(1, &idlong, &x, &y);
  5. }

最后调用GLView::handleTouchesBegin 通过 EventDispatcher::dispatchEvent进行事件的分发,调用事件响应函数,都是C++里完成的,就不再往下分析了。

能力有限,分析有误处请指正。

cocos2dx android平台事件系统解析的更多相关文章

  1. Cocos2d-x 3.2 学习笔记(四)学习打包Android平台APK!

    从cocos2dx 3.2项目打包成apk安卓应用文件,搭建安卓环境的步骤有点繁琐,但搭建一次之后,以后就会非常快捷! (涉及到3.1.1版本的,请自动对应3.2版本,3.x版本的环境搭建都是一样的) ...

  2. Cocos2d-x 3.2 打包Android平台APK

    (转自:http://www.cnblogs.com/Richard-Core/p/3855130.html) 从cocos2dx 3.2项目打包成apk安卓应用文件,搭建安卓环境的步骤有点繁琐,但搭 ...

  3. Cocos2d-x 3.0修改Android平台帧率fps - 解决游戏运行手机发热发烫问题

    使用Cocos2d-x 3.0开发游戏之后,发现游戏在android手机上发热非常严重,在魅族2上,几乎担心手机会爆炸了~~~采取的一个措施就是降低帧率,因为游戏对于帧率要求不是非常高. 做过coco ...

  4. 关于文章“cocos2dx移植android平台-我的血泪史”需要注意事项

    关于文章"cocos2dx移植android平台-我的血泪史"需要注意事项 在上次转载的这篇文章中,按照配置一步一步的下去.发现工程中在Android.mk中有一处错误.直接bui ...

  5. cocos2dx使用了第三方库照样移植android平台-解决iconv库的移植问题

    当我写这篇文章的时候我是怀着激动的心情的,因为我又解决了一个技术问题.你可能对题目还一知半解,这是什么意思,我之所以要写这篇文章就是要解决当我们在cocos2dx中使用了第三方库的时候,移植到andr ...

  6. cocos2dx移植android平台-我的血泪史

    版权声明:本文由( 小塔 )原创,转载请保留文章出处! 本文链接:http://www.zaojiahua.com/android-platform.html 本人这几天一直都没有跟新自己的网站内容, ...

  7. TalkingData Cocos2dx在android平台使用总结

    前言:最近发现很多朋友在使用TalkingData游戏版本Cocos2dx SDK使用过程中会出现的一些问题,今天来做一下总结,希望对您有所帮助: 首先非常感谢您使用TalkingData游戏统计平台 ...

  8. mac下面xcode+ndk7配置cocos2dx & box2d的跨ios和android平台的游戏教程

    这篇教程是介绍如何使用cocos2d-x和box2d来制作一个demo,且此demo能同时运行于ios和android平台.在继续阅读之前,建议您先阅读上一篇教程. 首先,按照上一篇教程,搭建好mac ...

  9. 为Cocos2d-x的Android平台加入Protobuffer支持

    为Cocos2d-x的Android平台加入Protobuffer支持 分类: 工作2013-11-27 18:00 386人阅读 评论(1) 收藏 举报 cocos2d-xandroid平台交叉编译 ...

随机推荐

  1. 高灵活低耦合Adapter快速开发攻略

    Android开发中经常需要使用Adapter. 传统方法是自定义一个Adapter并继承AndroidSDK内的BaseAdapter, 这种方式代码量大,耦合度高,灵活性差(各种监听事件需要对Vi ...

  2. NekoHTML and Dom4j

    http://pro.ctlok.com/2010/07/java-read-html-dom4j-nekohtml.html package com.ctlok.pro; import java.i ...

  3. 【转】Split strings the right way – or the next best way

      I know many people are bored of the string splitting problem, but it still seems to come up almost ...

  4. 关于Git的暂存区这个概念的理解.

    Git中的暂存区成为stage或者是index.可以理解成一个"提交任务".Git暂存区是Git最成功的设计之一,但是也是最难理解的. 暂存区是一个介于工作区和版本库的中间状态.当 ...

  5. CentOS下modelsim 10.2c install & crack

    install: 1. install is easy to all : run install.linux 2 crack: this section is important: a. instal ...

  6. Rshare Pro是否可以放入至客户企业App Store?

    现在很多客户内部部署了苹果授权的企业内部的AppStore,我们的Rshare Pro 是完全允许放入企业搭建的AppStore平台中.但每份需要收费20美元,换成人民币是120元.

  7. EditPlus自动补全、模板配置

    EditPlus真的是一款非常好用的编辑器,虽然小,但是短小精悍,速度快.只要配置好了,功能也是很强大的.下面来总结一下如何配置EditPlus的自动补全,和模板配置. 一.配置自动补全:(以开发CS ...

  8. 预算oracle

    select * from ( )) CODE_VERSION from ( SELECT tb_cube_fc05.pk_entity pk_org,/*主体pk*/ org_orgs.code o ...

  9. Asp.net MVC 4 视图相关和其他

    @{ Layout = “…”} To define layout page Equivalent to asp.net master-page 要定义相当于ASP.NET母版页的页面布局 @mode ...

  10. zxing.dll生成条码

    引入zxing.dll using System; using System.Drawing; using ZXing.QrCode; using ZXing; using ZXing.Common; ...