How to disable MultiTouch in Cocos2d-x (Android)

Aleksei Pinchuk
Sep 10, 2018 · 2 min read

There was one problem in Cocos2d-x (before version 3.14). If you wanted to use single touch in Android you must comment the code into Cocos2d-x. But when you will replace Cocos2d-x with the new version, you must remember to comment the code again. This article will answer to how disabled multitouch for all version of Cocos2d-x without commenting the code.

How to disable multitouch by commenting the code discussed here.

It is logical to assume that in order to solve this problem it is necessary to remove all touches with id greater than 0. How it to do?

In Android all touches go through Cocos2dxGLSurfaceView. Let’s consider the case with action touch down.

We need to use the touch with idDown == 0 only. But there is one problem. mCocos2dxRenderer is a private member and when we inherit Cocos2dxGLSurfaceView, we can’t use mCocos2dxRenderer.

Let’s consider Cocos2dxRender methods handleActionDown, handleActionMove, handleActionUp and handleActionCancel.

This methods use static methods of Cocos2dxRender:

Fortunately, we use Java. In Java we can get access to private method. Let’s write the method for access to private method.

Now we should inherit Cocos2dxGLSurfaceView, use static methods of Cocos2dxRender and cut touches. Full code of MyCocos2dxGLSurfaceView:

And we should change method onCreateView in

After discussing this problem, Cocos2d-x developers added a method setMultipleTouchEnabled in Cocos2dxGLSurfaceView in version ≥3.14. And now the single touch is done like this:

Ok. We have a single touch for different versions of the Cocos2d-x.

how hackers start their afternoons.

Aleksei Pinchuk

Written by

how hackers start their afternoons.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade