Cocos2d-js Touch Event

Source: Internet
Author: User
Tags event listener
The time aspect of the touch event there are two event listeners: CC. Eventlistener.touch_one_by_one and Cc.EventListener.TOUCH_ALL_AT_ONCE, respectively, correspond to a single point of touch and multi-touch. These listeners have touch event response properties that correspond to different stages of the touch event. By setting these properties, you can implement the association of events with event handler functions. The response properties of a single point touch event are as follows: (1) Ontouchbegan. The function specified by the function is recalled when a finger touches the screen. If the function return value is true, then the function specified by the following two properties (Ontouchmoved and ontouchended) can be recalled, otherwise no callback. (2) ontouchmoved. When a finger moves on the screen 2 back the function specified by the property (3) ontouchended. Callback the function specified by the property when a finger leaves the screen. (4) ontouchcancelled. Callback the function specified by the property when a single point touch event is canceled. The response properties of the Multi-Touch event are as follows: (1) Ontouchesbegan. Callback the function specified by the property (2) ontouchesmoved when multiple fingers touch the screen. Callback the function specified by the property (3) ontouchesended when multiple fingers move on the screen. Callback the function specified by the property (4) outouchescancelled When multiple fingers leave the screen. Callback the function specified by the property when the Multi-Touch event is canceled. The code fragments that use these properties demonstrate their use of var listener = cc. Eventlistener.create ({event:cc. Eventlistener.touch_one_by_one, Ontouchbegan:function (touch,event) {... return false;}}); First you need to use CC. The Eventlistener.create () function creates an event listener object. Then set its event property to CC. Eventlitener. Touch_one_by_one, expressed as a listener single point touch event. Set the Ontouchbegan property response to the finger touch screen stage action. Other phase actions of touch events also need to adopt similar code. The space aspect of touch events is each touch point (CC. Touch) object contains the current position information, as well as the previous location information. The following function can get the location information before the touch point. [CC. Point]getprevioUslocationinview ()//ui coordinates [CC. Point]getpreviouslocation ()          //OPENGL coordinates the following function to obtain the current location of the touch point information. [CC. Point]getlocationinview ()              //UI coordinates [CC. Point]getlocation ()                        //OPENGL coordinates single point touch event
Line 1th of the preceding code is to create a single point of touch Event listener object. The 2nd line of code is whether the setting swallows the event, if set to true. Then swallow the event when the Ontouchbegan function returns True. Event is not passed to the next node object. The 3rd line of code is to set the listener's Ontouchbegan property callback function. The 10th line of code is to set the listener's Ontouchended property callback function. The 4th line of code is the wizard object that gets the event bound. where the Event.getcurrenttarget () statement return value is the node object. The 5th line of code is to obtain the model coordinates of the current touch point relative to the target object. The 6th line of code is the size of the target object. The 7th line of code is to create a rect variable from the size of the target object

The 8th line of code is to determine whether the touch point is within the target object. The 9th line of code is to enlarge the target object, where the code 第12-14 line is the registered listener, where line 12th uses the wizard to display the priority registration event listener, where the parameter Getchildbytag (kboxa_tag) is the wizard tag implementation to get the Sprite object. Lines 13th and 14th are for two other sprites to register the event listener, where Listerner.clone () obtains the listener object, using the Clone () function because each event listener can only be registered once, AddEventListener will set up a registration identity when registering the event listener. Once the registration identity is set, the listener can no longer be used to register other event listeners. It is therefore necessary to clone a new listener object using Listener.clone () and use the new listener object for registration. Multi-touch event multi-touch events are related to specific platforms, and multi-touch and single touch development processes are basically similar.
The first line ' touches ' in cc.sys.capabilities determines whether the current system and device support multi-touch. 2–5 Line code is a quick way to register event listeners to the manager. The main code is as follows: Cc.eventManager.addListener ({...},this) uses this shortcut to register an event listener without creating a EventListener object.
The 3rd line of code is to set the listener's Outouchbegan property callback line 6th function, and the 4th line of code is to set the listener's Ontouchmoved property callback 9th line function. The 5th line of code is to set the listener's Outouchended property callback line 10th function. Line 7th code var touch=toucher[i] Take the 8th line of code from the touches var id=touch.getid () to get the touch point ID, the same ID description is the same touch point.

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.