How to disable MultiTouch in cocos2d-x

Hi,
i want to disable multiTouch, is there any method for that

Thanks,
Gurusagar

1 Like

It is platform specific. In iOS, you should enable it by yourself.
In Android, as I know, it can not disable it if the system supports multitouch.

Can you add this feature in cocos?

what is there to add? You can enable or disable in AppController

I personally enable / disable it depending on the platform. On iOS you can easily do that with [enableMultiTouch:YES/NO]; in android you can refer to the last post in this thread:

It really works!

@slackmoehrle, is there a way to turn it on/off with a c++ call?

Best,

Davide

Not that I know off. I don’t see anything in Director.

@davidejones88 because not all platforms supports multi-touch, and not all mobile platforms can disable multi-touch. So we left it to developers.

Ok. I want to use cocos2dx static library without changes in cocos. I can ignore the touches with id no equal 0. But EventDispatcher hash’t virtual function. I can inherit GLView and ignore some touches, but GLView created in cocos java files. But i want to make static library of cocos without changes inside. Can you add same virtual function in EventDispatcher? It will be easy way. You also can add function setSingleTouch(bool) and ignore all touches with id not equal 0, it will be work on all platform. if you don’t want to do this function, give me a chance to do it yourself.

for example:
VIRTUAL void dispatchTouchEvent(EventTouch* event);

Thanks for the explanation! Makes sense!

Best,

Davide

@Pearson i think you can

  • inherit Cocos2dxGLSurfaceView and override onTouchEvent
  • inherit Cocos2dxActivity and override onCreateView

Both these classes are in java side.

oh, thank you!
It’s still a difficult way. Forced to write platform code. But it can be done.

/****************************************************************************
Copyright (c) 2010-2011 cocos2d-x.org

http://www.cocos2d-x.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 ****************************************************************************/
package org.cocos2dx.cpp;

import android.app.Activity;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;

public class MyCocos2dxGLSurfaceView extends Cocos2dxGLSurfaceView {
    
    // ===========================================================
    // Constructors
    // ===========================================================
    
    public MyCocos2dxGLSurfaceView(final Context context) {
        super(context);
        
        this.initView();
    }
    
    public MyCocos2dxGLSurfaceView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        
        this.initView();
    }
    
    @Override
    public boolean onTouchEvent(final MotionEvent pMotionEvent) {
        // these data are used in ACTION_MOVE and ACTION_CANCEL
//        final int pointerNumber = pMotionEvent.getPointerCount();
        final int pointerNumber = 1;
        final int[] ids = new int[pointerNumber];
        final float[] xs = new float[pointerNumber];
        final float[] ys = new float[pointerNumber];
        
        if (isSoftKeyboardShown()){
            InputMethodManager imm = (InputMethodManager)this.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            View view = ((Activity)this.getContext()).getCurrentFocus();
            imm.hideSoftInputFromWindow(view.getWindowToken(),0);
            this.requestFocus();
            setSoftKeyboardShown(false);
        }
        
        for (int i = 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) {
            case MotionEvent.ACTION_POINTER_DOWN:
                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() {
                        MyCocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleActionDown(idPointerDown, xPointerDown, yPointerDown);
                    }
                });
                break;
                
            case MotionEvent.ACTION_DOWN:
                // 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() {
                        MyCocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleActionDown(idDown, xDown, yDown);
                    }
                });
                break;
                
            case MotionEvent.ACTION_MOVE:
                this.queueEvent(new Runnable() {
                    @Override
                    public void run() {
                        MyCocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleActionMove(ids, xs, ys);
                    }
                });
                break;
                
            case MotionEvent.ACTION_POINTER_UP:
                final int indexPointUp = pMotionEvent.getAction() >> MotionEvent.ACTION_POINTER_INDEX_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() {
                        MyCocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleActionUp(idPointerUp, xPointerUp, yPointerUp);
                    }
                });
                break;
                
            case MotionEvent.ACTION_UP:
                // there are only one finger on the screen
                final int idUp = pMotionEvent.getPointerId(0);
                final float xUp = xs[0];
                final float yUp = ys[0];
                
                this.queueEvent(new Runnable() {
                    @Override
                    public void run() {
                        MyCocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleActionUp(idUp, xUp, yUp);
                    }
                });
                break;
                
            case MotionEvent.ACTION_CANCEL:
                this.queueEvent(new Runnable() {
                    @Override
                    public void run() {
                        MyCocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleActionCancel(ids, xs, ys);
                    }
                });
                break;
        }
        
        /*
         if (BuildConfig.DEBUG) {
         Cocos2dxGLSurfaceView.dumpMotionEvent(pMotionEvent);
         }
         */
        return true;
    }

}

I can’t override onTouchEvent in Cocos2dxGLSurfaceView
mCocos2dxRenderer has private access in Cocos2dxGLSurfaceView

@Pearson sorry about it. I think you can change it to protected. And you are appreciated to send a PR for it.