Multitouch and Buttons (Hack to turn off Multitouch)

I have the current cenario:
1 scene with 2 ccui.Buttons each one goes to different scenes.
In android with two fingers i click in both of then at the same time.
The result both scenes will be in my scene stack.

Futter investigation show that ccui.Button does not care about the touch ID so the fingers 0 and 1 can trigger two buttons in the same page.

So i implemented this code to swallow every touch other than the very first one (touch.getID() === 0)

input.SingleTouch.setEnable (true); // Single Touch
input.SingleTouch.setEnable (false); // Multi Touch

var input = input || {};

// this is needed because no matter that ID the touch can trigger the ccui.Button click event
input.SingleTouch = function () {
	this.enabled = false;
	var self = this;
	
	var blockEveryTouchOtherThanFirstOne = function (touch, event) {
		// blocks every touch other than the frist one
		if (self.enabled) {
			if (touch.getID () !== 0) {
				cc.log ("Swallow touch " + touch.getID ());
				return true;
			}
		}

		return false;
	};
	
	this.listener = cc.EventListener.create({
		event: cc.EventListener.TOUCH_ONE_BY_ONE,
		swallowTouches: true,
		onTouchBegan: blockEveryTouchOtherThanFirstOne,
		onTouchesMoved: blockEveryTouchOtherThanFirstOne,
		onTouchesEnded: blockEveryTouchOtherThanFirstOne,
		onTouchesCancelled: blockEveryTouchOtherThanFirstOne
	});
	
	cc.eventManager.addListener(this.listener, -1);
};

input.SingleTouch.prototype.setEnable = function (enable) {
	this.enabled = enable;
};

input.SingleTouch.instance = null;

input.SingleTouch.getInstance = function () {
	if (!input.SingleTouch.instance) {
		input.SingleTouch.instance = new input.SingleTouch ();
	}
	return input.SingleTouch.instance;
};

input.SingleTouch.setEnable = function (enable) {
	input.SingleTouch.getInstance().setEnable (enable);
};

Enjoy :wink: !

1 Like

Dude you are awesome! Thanks for sharing this code, it works perfectly.