Touch events in Cocos2d-x

Source: Internet
Author: User
Understanding a touch event can be considered in terms of time and space.

1. Touch Event Time

As shown in the time aspect of a touch event, there can be different stages such as "Press", "move", and "lift, indicates whether the touch is just starting, moving, or still, and when it ends, that is, when the finger is lifted from the screen. In addition, single-point touch or multi-point touch can be performed at different stages of a touch event. Whether multi-point touch is supported depends on the device and platform.

 

A touch event has two event listeners: eventlistenertouchonebyone and eventlistenertouchallatonce, which correspond to single-point touch and multi-point touch respectively. These listeners have some touch Event Response attributes, which correspond to different stages of the touch event. By setting these attributes, You can associate events with event handlers.

Touch Event Response attributes in eventlistenertouchonebyone:

STD: function <bool (touch *, event *)> ontouchbegan. Calls back the function specified by this attribute when a finger touches the screen. If the return value of the function is true, you can call back the functions specified by the following two attributes (ontouchmoved and ontouchended). Otherwise, no callback is performed.

STD: function <void (touch *, event *)> ontouchmoved. When a finger moves on the screen, it calls back the specified function of this attribute.

STD: function <void (touch *, event *)> ontouchended. When a finger leaves the screen, it calls back the specified function of this attribute.

STD: function <void (touch *, event *)> ontouchcancelled. When a single touch event is canceled, the specified function of this attribute is called back.

 

Description: STD: function is a universal function encapsulation. STD: an instance of a function can be any callable target, including a function, Lambda expression, binding expression, and other function objects.

Touch Event Response attributes in eventlistenertouchallatonce:

STD: function <void (conststd: vector <touch *> &, event *)> ontouchesbegan. The function specified by this attribute is called back when multiple fingers touch the screen.

STD: function <void (conststd: vector <touch *> &, event *)> ontouchesended. The function specified by this attribute is called back when multiple fingers move on the screen.

STD: function <void (conststd: vector <touch *> &, event *)> ontouchesmoved. The function specified by this attribute is called back when multiple fingers exit the screen.

STD: function <void (conststd: vector <touch *> &, event *)> ontouchescancelled. When a multi-touch event is canceled, the function specified by this attribute is called back.

 

Code snippets using these attributes demonstrate their usage:

 

[HTML]View plaincopy

 

  1. Auto listener = eventlistenertouchonebyone: Create ();

  2. Listener-> ontouchbegan = cc_callback_2 (helloworld: touchbegan, this );

  3. ......

  4. Bool helloworld: touchbegan (touch * touch, event * event ){

  5. ......

  6. Returnfalse;

  7. }

 

 

First, we need to use eventlistenertouchonebyone: Create () to create an independent touch event listener object. Then set its listener-> ontouchbegan attribute, where cc_callback_2 (helloworld: touchbegan, this) is used to bind the callback function with the cc_callback_2 macro, which is the bool helloworld defined below :: touchbegan (touch * touch, event * event) function.

Similar code is also required for other touch events.

 

2. Spatial aspects of touch events

In terms of space, each touch object contains the current location information and the previous location information (if any). The following function can obtain the location information before the touch point:

Point getpreviuslocationinview () // UI Coordinate

Point getpreviuslocation () // OpenGL Coordinate

The following function obtains the current position of a touch point.

Point getlocationinview () // UI Coordinate

Point getlocation () // OpenGL coordinates

 

 

 

More content please pay attention to the Cocos2d-x series of books "Cocos2d-x practice (Volume I): c ++ Development"

This book discussion site: http://www.cocoagame.net

Welcome to cocos2d-x Technology Discussion Group: 257760386, 327403678


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.