How can I undo the one touch that is executed in cocos2d

I am working on a game in which the user drags an object. I want to stop moving the object when an event occurs, when the user is still trying to drag the object. Is there any way to do this? Basically cancel the touch that is currently running without affecting other touch events that may occur, as this is a multi-touch application.

I am using cocos2d v1.1.0 and using ccTouchesBegan and ccTouchesMoved.

Thanks for any suggestions you may have.

+4
source share
3 answers

Paste the following codes in which you want to cancel touch events.

[[[CCDirector sharedDirector] touchDispatcher] setDispatchEvents:NO]; 

Or obsolete method

 [[CCTouchDispatcher sharedDispatcher] setDispatchEvents:NO]; 
+5
source

Why not just do it with the flag on the draggable object?

 - (void)ccTouchesBegan... { touchedObject.canDrag = YES; } - (void)ccTouchesMoved... { if (touchedObject.canDrag) { //Drag } } - (void)eventThatStopsDrag { touchedObject.canDrag = NO; } 
+2
source

If you use cocos2dx-js, you can write this code in main.js

When your mouse cursor goes outside the window, it dispatches a mouseup event. Its most useful when considering a script, you drag any sprite and want to cancel the mousemove event when the cursor leaves the window.

 cc.game.onStart = function{ ....................... ....................... ....................... var prohibition = false; if( cc.sys.isMobile) prohibition = true; var selfPointer = cc.inputManager; var element = cc._canvas; element.addEventListener("mouseout", function (event) { if(prohibition) return; selfPointer._mousePressed = false; var pos = selfPointer.getHTMLElementPosition(element); var location = selfPointer.getPointByEvent(event, pos); selfPointer.handleTouchesEnd([selfPointer.getTouchByXY(location.x, location.y, pos)]); var mouseEvent = selfPointer.getMouseEvent(location,pos,cc.EventMouse.UP); mouseEvent.setButton(event.button); cc.eventManager.dispatchEvent(mouseEvent); event.stopPropagation(); event.preventDefault(); }); ....................... ....................... ....................... } 
0
source

Source: https://habr.com/ru/post/1435388/


All Articles