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