Cocos2dx-lua several ways to capture user touch events

Source: Internet
Author: User

This is only for Lua.
1. Register callback functions for each event of concernSpecifically divided into the following several 1> single touch registration function is CC. Handler.event_touch_began = 40Acl handler.event_touch_moved = 41cc. handler.event_touch_ended = 42cc. handler.event_touch_cancelled = 43 must pass CC when registering. Eventlistenertouchonebyone:create () Create a callback function code that listenerontouchbegin/ontouchmove/ontouchend registers for itself as follows: local listener = Acl Eventlistenertouchonebyone:create (); Listener:registerscripthandler (ONTOUCHBEGIN,CC. Handler.event_touch_began); Listener:registerscripthandler (ONTOUCHMOVE,CC. handler.event_touch_moved); Listener:registerscripthandler (ONTOUCHEND,CC. handler.event_touch_ended);

2> Multi-point touch cc. Handler.event_touches_began = 44Cc. handler.event_touches_moved = 45Cc. handler.event_touches_ended = 46cc. Handler.event_touches_cancelled = 47 must pass CC when registering. Eventlistenertouchallatonce:create () Create a callback function code that listenerontouchesbegin/ontouchesmove/ontouchesend registers for itself as follows: Local Listener = cc. Eventlistenertouchallatonce:create (); Listener:registerscripthandler (ONTOUCHESBEGIN,CC. Handler.event_touches_began); Listener:registerscripthandler (ONTOUCHESMOVE,CC. handler.event_touches_moved); Listener:registerscripthandler (ONTOUCHESEND,CC. handler.event_touches_ended);
Finally, bind LISTENERCC with the following code. Director:getinstance (): Geteventdispatcher (): Addeventlistenerwithscenegraphpriority (Listener,_layer), where _ Layer is the object in front of which you want the event to be CC. Director:getinstance () can also be used as an object of the parent node of _layer or _layer
Here are a few things to note: 1.ontouchesbegin/ontouchbegin need to return true, indicating the need to deal with this event, or will not be dropped Ontouchmove/ontouchend
2. Each touch function contains 2 parameters with Ontouchmove as an example: local function Ontouchmove (touch,event) endtouch/event are UserData (can be understood as C/lua common data As long as the corresponding method is implemented C/lua can directly access/assign/call functions to manage this memory by C)
Touch is the current point of contact below is its method/** returns the current touch location in opengl  coordinates.**  @return  The current touch location in OpenGL  Coordinates.*/vec2 getlocation ()  const;/** Returns the previous touch  location in opengl coordinates.**  @return  the previous touch location  in opengl coordinates.*/vec2 getpreviouslocation ()  const;/** returns the  start touch location in OpenGL coordinates.**  @return  the start  touch location in opengl coordinates.*/vec2 getstartlocation ()  const;/**  returns the delta of 2 current touches locations in screen  coordinates.**  @return  the delta of 2 current touches locations  in screen coordinAtes.*/vec2 getdelta ()  const;/** returns the current touch location in  screen coordinates.**  @return  The current touch location in  Screen coordinates.*/vec2 getlocationinview ()  const;/** returns the previous  touch location in screen coordinates. **  @return  the previous  touch location in screen coordinates.*/vec2 getpreviouslocationinview ()   const;/** returns the start touch location in screen coordinates.**   @return  the start touch location in screen coordinates.*/ As the following code can be directly in the Ontouchmove to get the user's finger move distance local dis = Touch:getdelta () print (DIS.X,DIS.Y); If Multitouch touch is a tabletouch[1]  TOUCH[2] TOUCH[3] ... is the corresponding point of the touch
The event can indicate that 1. The current user clicked on the object (Event:getcurrenttarget ()) 2. The current state of Press/move/release (Event:geteventcode ()) cc. EventCode ={began = 0, MOVED = 1, ENDED = 2, CANCELLED = 3,} So we can use a callback function to determine the current operation without writing 3 functions.
Sample code
Local function Ontouchbegin (touch,event) Local p = touch:getlocation ();     p = _layer:converttonodespace (p); Print (P.X,P.Y) return true;end
Local function Ontouchmove (touch,event) End
Local function Ontouchend (touch,event) End
Local function Ontouchesbegin (touch,event) return true;end
Local function Ontouchesmove (touch,event)
For i = 1,TABLE.GETN (touch) does local location = Touch[i]:getlocation () print (I,LOCATION.X,LOCATION.Y) End endlocal function ontouchesend (touch,event) print ("Ontouchesend"); end
_layer = cc.    Layer:create (); _layer:settouchenabled (true) Local listener1 = cc.    Eventlistenertouchonebyone:create (); Listener1:registerscripthandler (ONTOUCHBEGIN,CC.    Handler.event_touch_began); Listener1:registerscripthandler (ONTOUCHMOVE,CC.    handler.event_touch_moved); Listener1:registerscripthandler (ONTOUCHEND,CC. handler.event_touch_ended);
--Multi-touch-local listener2 = CC. Eventlistenertouchallatonce:create ()--listener2:registerscripthandler (ONTOUCHESBEGIN,CC. Handler.event_touches_began)--Listener2:registerscripthandler (ONTOUCHESMOVE,CC. handler.event_touches_moved)--Listener2:registerscripthandler (ONTOUCHESEND,CC. handler.event_touches_moved)
Local eventdispatcher = _layer:geteventdispatcher () eventdispatcher:addeventlistenerwithscenegraphpriority ( Listener1, _layer)--eventdispatcher:addeventlistenerwithscenegraphpriority (Listener2, _layer)

2. Look directly at the codeLocal function ontouchevent (state, ...)        Local args = {...}; Print (state);
For k,v in pairs (args[1]) do print (K,V) end end
_layer:registerscripttouchhandler (Ontouchevent,true,0,false);
@onTouchEvent callback @ture means capture to capture multi-touch @0 priority @false whether to engulf touch event if it is a single touch args[1]->x,y,id If it is multi-touch args[1]->x1,y1,id1,x2,y2, Id2
Here Cocos2dx-lua encapsulates Layer:ontouch (callback, Ismultitouches, swallowtouches) recommended for direct use

Here, if you use Cocos2dx-lua's lua-empty-test to create a project, there's a bug that needs to add code to didfinishlaunchingwithoptions [Eaglview Setmultipletouchenabled:yes]; To open the multi-touch. I wasted half a day. Event Debug Fuck

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Cocos2dx-lua several ways to capture user touch events

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.