Several ways for cocos2dx-lua to capture user touch events

Source: Internet
Author: User

Several ways for cocos2dx-lua to capture user touch events
Here, only for lua
1. registering callback functions for each event is divided into the following types: 1> registering a single point of touch function as cc. handler. EVENT_TOUCH_BEGAN = 40 cc. handler. EVENT_TOUCH_MOVED = 41 cc. handler. EVENT_TOUCH_ENDED = 42 cc. handler. EVENT_TOUCH_CANCELLED = 43 cc must be used for registration. eventListenerTouchOneByOne: create () create listener onTouchBegin/onTouchMove/onTouchEnd as the callback function code registered by yourself: local listener = cc. 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> click cc. handler. EVENT_TOUCHES_BEGAN = 44 cc. handler. EVENT_TOUCHES_MOVED = 45 cc. handler. EVENT_TOUCHES_ENDED = 46 cc. handler. EVENT_TOUCHES_CANCELLED = 47 must pass cc when registering. eventListenerTouchAllAtOnce: create () create the listener onTouchesBegin/onTouchesMove/onTouchesEnd callback function code for self-registration: 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 the listener cc using the following code. director: getInstance (): getEventDispatcher (): addEventListenerWithSceneGraphPriority (listener, _ layer); where _ layer is cc before the object needing an event. director: getInstance () can also be replaced with an object of the _ layer or _ layer parent node.
Note the following points: 1. In onTouchesBegin/onTouchBegin, return true is required, indicating that the event must be handled. Otherwise, onTouchMove/onTouchEnd will not be used.
2. each touch function contains two parameters. Take onTouchMove as an example: local function onTouchMove (touch, event) end touch/event are all userdata (it can be understood that the data shared by C/LUA can be directly accessed/assigned/called by C as long as the corresponding method is implemented)
Touch is the current touch point. the following 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 getpreviuslocation () 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 getpreviuslocationinview () const;/** Returns the start touch location in screen coordinates. ** @ return The start touch location in screen coordinates. */The following code can be used to directly obtain the movement distance from the user's finger in onTouchMove: local dis = touch: getDelta () print (dis. x, dis. Y); if it is multi-point touch, it is a table touch [1] touch [2] touch [3]… Is the corresponding point of touch
Event can indicate 1. the current user clicks the object (event: getCurrentTarget () 2. the current status 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, instead of writing three 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) do local location = touch [I]: getLocation () print (I, location. x, location. y) end local 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-point 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: Listen (listener2, _ layer)

2. Check the code local function onTouchEvent (state,...) local args = {...}; print (state );
For k, v in pairs (args [1]) do print (k, v) end
_ Layer: registerScriptTouchHandler (onTouchEvent, true, 0, false );
@ OnTouchEvent callback @ ture indicates capture to capture multi-point touch @ 0 priority @ false whether to swallow touch event if it is a single-point touch args [1]-> x, y, id if it is multi-point touch args [1]-> x1, y1, id1, x2, y2, id2
Here the cocos2dx-lua encapsulates Layer: onTouch (callback, isMultiTouches, swallowTouches), which is recommended for direct use


Here if you use cocos2dx-lua's lua-empty-test as the template to build a project there is a bug that needs to add code in didfinishlaunchingwitexceptions [eaglView setMultipleTouchEnabled: YES]; I wasted half a day to debug FUCK.





 

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: info-contact@alibabacloud.com 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.