前端之家收集整理的这篇文章主要介绍了
cocos2dx 点击事件分析(2),
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<pre name="code" class="cpp">1、java端 Cocos2dxGLSurfaceView类
@Override
public boolean onTouchEvent(final MotionEvent pMotionEvent) {
// these data are used in ACTION_MOVE and ACTION_CANCEL **
//触摸点的个数,也可以理解为多少个手指点击屏幕
final int pointerNumber = pMotionEvent.getPointerCount();
final int[] ids = new int[pointerNumber];
final float[] xs = new float[pointerNumber];
final float[] ys = new float[pointerNumber];
for (int i = 0; i < pointerNumber; i++) {
//每个手指都会分配一个唯一的ID,第一个id为0,依次累加1,2..
//只要手指没有离开屏幕,那么在移动过程中,这个ID不变,都是唯一的
//所以我们可以根据这个ID来判断手指,而且每个手指移动,都有响应的
//x,y坐标,不同手指之间的坐标无关
ids[i] = pMotionEvent.getPointerId(i);
xs[i] = pMotionEvent.getX(i);
ys[i] = pMotionEvent.getY(i);
}
switch (pMotionEvent.getAction() & MotionEvent.ACTION_MASK) {
//跟多点触控有关,只有当手指个数大于1个时,才会触发,每增加一个手指
//就会多执行一次,每多一个手指,都没分配一个唯一的ID(idPointerDown)
//因为第二个手指也会触发类似于第一个手指的点击动作,所以即使处理
//单点点击事件,也需要考虑第二个手指的点击动作。
case MotionEvent.ACTION_POINTER_DOWN:
final int indexPointerDown = pMotionEvent.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT;
final int idPointerDown = pMotionEvent.getPointerId(indexPointerDown);
final float xPointerDown = pMotionEvent.getX(indexPointerDown);
final float yPointerDown = pMotionEvent.getY(indexPointerDown);
this.queueEvent(new Runnable() {
@Override
public void run() {
Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleActionDown(idPointerDown,xPointerDown,yPointerDown);
}
});
break;
case MotionEvent.ACTION_DOWN: //单点触控
// there are only one finger on the screen
//单点触控,也可以理解为,第一个手指触摸屏幕时会触发这个事件,
//如果第二个手指点击屏幕,则不会触发这个事件,第二个手指即后面的
//会触发ACTION_POINTER_DOWN事件
final int idDown = pMotionEvent.getPointerId(0);
final float xDown = xs[0];
final float yDown = ys[0];
this.queueEvent(new Runnable() {
@Override
public void run() {
Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleActionDown(idDown,xDown,yDown);
}
});
break;
case MotionEvent.ACTION_MOVE:
//手指在屏幕上移动时,(由于屏幕很灵敏,即使点击也会伴随着触发这个动作)
//会触发这个事件,handleActionMove函数会把所有手指的移动事件,都传递给C++端
this.queueEvent(new Runnable() {
@Override
public void run() {
Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleActionMove(ids,xs,ys);
}
});
break;
case MotionEvent.ACTION_POINTER_UP:
final int indexPointUp = pMotionEvent.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT;
final int idPointerUp = pMotionEvent.getPointerId(indexPointUp);
final float xPointerUp = pMotionEvent.getX(indexPointUp);
final float yPointerUp = pMotionEvent.getY(indexPointUp);
this.queueEvent(new Runnable() {
@Override
public void run() {
Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleActionUp(idPointerUp,xPointerUp,yPointerUp);
}
});
break;
case MotionEvent.ACTION_UP:
// there are only one finger on the screen
//第一个手指离开屏幕会触发这个事件,如果有第二个手指,
//也离开屏幕,则会触发ACTION_POINTER_UP事件
final int idUp = pMotionEvent.getPointerId(0);
final float xUp = xs[0];
final float yUp = ys[0];
this.queueEvent(new Runnable() {
@Override
public void run() {
Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleActionUp(idUp,xUp,yUp);
}
});
break;
case MotionEvent.ACTION_CANCEL:
this.queueEvent(new Runnable() {
@Override
public void run() {
Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleActionCancel(ids,ys);
}
});
break;
}
/*
if (BuildConfig.DEBUG) {
Cocos2dxGLSurfaceView.dumpMotionEvent(pMotionEvent);
}
*/
return true;
}
-->>调用相应的jni方法:
public void handleActionDown(final int pID,final float pX,final float pY) {
Cocos2dxRenderer.nativeTouchesBegin(pID,pX,pY);
}
public void handleActionUp(final int pID,final float pY) {
Cocos2dxRenderer.nativeTouchesEnd(pID,pY);
}
public void handleActionCancel(final int[] pIDs,final float[] pXs,final float[] pYs) {
Cocos2dxRenderer.nativeTouchesCancel(pIDs,pXs,pYs);
}
public void handleActionMove(final int[] pIDs,final float[] pYs) {
Cocos2dxRenderer.nativeTouchesMove(pIDs,pYs);
}
2、C++中:TouchesJni.cpp文件
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesBegin(JNIEnv * env,jobject thiz,jint id,jfloat x,jfloat y) {
cocos2d::CCDirector::sharedDirector()->getOpenGLView()->handleTouchesBegin(1,&id,&x,&y);
}
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesEnd(JNIEnv * env,jfloat y) {
cocos2d::CCDirector::sharedDirector()->getOpenGLView()->handleTouchesEnd(1,&y);
}
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesMove(JNIEnv * env,jintArray ids,jfloatArray xs,jfloatArray ys) {
int size = env->GetArrayLength(ids);
jint id[size];
jfloat x[size];
jfloat y[size];
env->GetIntArrayRegion(ids,size,id);
env->GetFloatArrayRegion(xs,x);
env->GetFloatArrayRegion(ys,y);
cocos2d::CCDirector::sharedDirector()->getOpenGLView()->handleTouchesMove(size,id,x,y);
}
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesCancel(JNIEnv * env,y);
cocos2d::CCDirector::sharedDirector()->getOpenGLView()->handleTouchesCancel(size,y);
}
总结:手指ACTION_POINTER_DOWN和ACTION_DOWN事件一次只会触发一个,不会同时出现。
看cocos2d::CCDirector::sharedDirector()->getOpenGLView()->handleTouchesBegin(1,&y)
第一个参数表示num,始终为1.
原文链接:https://www.f2er.com/cocos2dx/343734.html