cocos2dx多个按钮同时响应点击事件

时间:2022-08-27 16:29:14

环境 cocos2d-x-3.1.1


iOS版本

找到 AppController.mm  

修改方法  -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions    中


[eaglView setMultipleTouchEnabled: YES]  改成 [eaglView setMultipleTouchEnabled: false]


Android版本

找到  

cocos2d-x\cocos\platform\android\Java\src\org\cocos2dx\lib\Cocos2dxGLSurfaceView.java

在方法  public boolean onTouchEvent(final MotionEvent pMotionEvent)  中添加两句

switch (pMotionEvent.getAction() & MotionEvent.ACTION_MASK) {
			case MotionEvent.ACTION_POINTER_DOWN:
				if (pointerNumber>1) return false;//add 屏蔽多点触摸
				final int indexPointerDown = pMotionEvent.getAction() >> MotionEvent.ACTION_POINTER_INDEX_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:
				if (pointerNumber>1) return false;//add 屏蔽多点触摸
				// there are only one finger on the screen
				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;