用户交互这里指的就是用户在手机上的点击,滑动以及晃动手机等行为,从而得到相应的反馈。今天学习Cocos2dx,遇到交互问题,所以就写出来和大家分享一下。我这里是以Android连接为例的,因为目前我只会Android相关的开发。好了,不多说,看下面步骤:
第一步:在Android中,交互操作的入口在SurfaceView或是GLSurfaceView中的onTouchEvent时间中。本例代码所在位置org.cocos2dx.lib---->Cocos2dxGLSurfaceView.java
- publicbooleanonTouchEvent(finalMotionEventpMotionEvent){@H_403_40@
- //thesedataareusedinACTION_MOVEandACTION_CANCEL@H_403_40@
- finalintpointerNumber=pMotionEvent.getPointerCount();@H_403_40@
- int[]ids=newint[pointerNumber];@H_403_40@
- float[]xs=float[pointerNumber];@H_403_40@
- float[]ys=float[pointerNumber];@H_403_40@
- @H_403_40@
- for(inti=0;i<pointerNumber;i++){@H_403_40@
- ids[i]=pMotionEvent.getPointerId(i);@H_403_40@
- xs[i]=pMotionEvent.getX(i);@H_403_40@
- ys[i]=pMotionEvent.getY(i);@H_403_40@
- }@H_403_40@
- switch(pMotionEvent.getAction()&MotionEvent.ACTION_MASK){@H_403_40@
- caseMotionEvent.ACTION_POINTER_DOWN:@H_403_40@
- intindexPointerDown=pMotionEvent.getAction()>>MotionEvent.ACTION_POINTER_INDEX_SHIFT;@H_403_40@
- intidPointerDown=pMotionEvent.getPointerId(indexPointerDown);@H_403_40@
- floatxPointerDown=pMotionEvent.getX(indexPointerDown);@H_403_40@
- floatyPointerDown=pMotionEvent.getY(indexPointerDown);@H_403_40@
- @H_403_40@
- this.queueEvent(newRunnable(){@H_403_40@
- @Override@H_403_40@
- voidrun(){@H_403_40@
- Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleActionDown(idPointerDown,xPointerDown,yPointerDown);@H_403_40@
- }@H_403_40@
- });@H_403_40@
- break;@H_403_40@
- caseMotionEvent.ACTION_DOWN:@H_403_40@
- //thereareonlyonefingeronthescreen@H_403_40@
- intidDown=pMotionEvent.getPointerId(0);@H_403_40@
- floatxDown=xs[0];@H_403_40@
- floatyDown=ys[0];@H_403_40@
- this.mCocos2dxRenderer.handleActionDown(idDown,xDown,yDown);@H_403_40@
- caseMotionEvent.ACTION_MOVE:@H_403_40@
- newRunnable(){@H_403_40@
- @Override@H_403_40@
- voidrun(){@H_403_40@
- Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleActionMove(ids,xs,ys);@H_403_40@
- });@H_403_40@
- break;@H_403_40@
- caseMotionEvent.ACTION_POINTER_UP:@H_403_40@
- intindexPointUp=pMotionEvent.getAction()>>MotionEvent.ACTION_POINTER_INDEX_SHIFT;@H_403_40@
- intidPointerUp=pMotionEvent.getPointerId(indexPointUp);@H_403_40@
- floatxPointerUp=pMotionEvent.getX(indexPointUp);@H_403_40@
- floatyPointerUp=pMotionEvent.getY(indexPointUp);@H_403_40@
- this.mCocos2dxRenderer.handleActionUp(idPointerUp,xPointerUp,yPointerUp);@H_403_40@
- caseMotionEvent.ACTION_UP:@H_403_40@
- //thereareonlyonefingeronthescreen@H_403_40@
- intidUp=pMotionEvent.getPointerId(0);@H_403_40@
- floatxUp=xs[floatyUp=ys[this.mCocos2dxRenderer.handleActionUp(idUp,xUp,yUp);@H_403_40@
- caseMotionEvent.ACTION_CANCEL:@H_403_40@
- this.mCocos2dxRenderer.handleActionCancel(ids,ys);@H_403_40@
- returntrue;@H_403_40@
- }@H_403_40@
第二步:Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleActionDown(idDown,yDown)等相关语句的方法在org.cocos2dx.lib------>Cocos2dxRender.java,代码如下:
copy
- privatestaticnativevoidnativeTouchesBegin(intid,floatx,153); background-color:inherit; font-weight:bold">floaty);@H_403_40@
- voidnativeTouchesEnd(floaty);@H_403_40@
- voidnativeTouchesMove(int[]ids,153); background-color:inherit; font-weight:bold">float[]xs,153); background-color:inherit; font-weight:bold">float[]ys);@H_403_40@
- voidnativeTouchesCancel(float[]ys);@H_403_40@
- voidhandleActionDown(floaty){@H_403_40@
- Cocos2dxRenderer.nativeTouchesBegin(id,x,y);@H_403_40@
- voidhandleActionUp( Cocos2dxRenderer.nativeTouchesEnd(id,153); background-color:inherit; font-weight:bold">voidhandleActionCancel(float[]ys){@H_403_40@
- Cocos2dxRenderer.nativeTouchesCancel(ids,ys);@H_403_40@
- voidhandleActionMove( Cocos2dxRenderer.nativeTouchesMove(ids,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> }@H_403_40@