[IOS-Cocos2d Game Development Five] multi contact and touch screen event details (single monitor, event distribution) [updated in November 28]

Source: Internet
Author: User

Li huaming himiOriginal, reprinted must be explicitly noted:
Reprinted from[Heimi gamedev block]Link: http://www.himigame.com/iphone-cocos2d/450.html


----------------------------------- This chapter begins with a supplement! ----------------------------------

This article has made a detailed description of the Multi-contact and touch-screen events, but a little bit ignored, that is, to enable multi-contact support! The procedure is as follows:

First, enter the appdelegate. M class,

- (void) applicationDidFinishLaunching:(UIApplication*)application{}

In the above method, add the following code to enable multi-contact support:

// Supports multiple contacts [viewcontroller. View setmultipletouchenabled: Yes];

Set multiple contacts for other classes:

[[Ccdirector shareddire] openglview] setmultipletouchenabled: Yes];

-------------------------- Supplemented in this Chapter ------------------------------------------------------------------------------


Cocos2d has been being honed over the past few days, and many things have been digested. You can basically grasp the game of a hand-written company. Today, I will share some important points with you to share my experience, provide new children's shoes as a reference;

This article will introduce cocos2d in detail to analyze the user's touch screen listening events (cocos2d has many detailed articles and tutorials, I am here for my own understanding)

Question: There are two types of event listening for touch screens in cocos2d:

1. for a single listener, the so-called single listener is actually related to the cocos2d engine framework. Because each game interface in cocos2d can be completed using a cclayout, when a cclayout is displayed on the screen, to listen to a user's key events, the following methods are generally used for listening: (Note: The cclayout class is used for listening)

Enable the listener first:

self.isTouchEnabled=YES;

Then rewrite the listening function:

// Listen to the first trigger event-(void) cctouchesbegan :( nsset *) touches withevent :( uievent *) event {} // touch event-when the finger moves on the screen-(void) cctouchesmoved :( nsset *) touches withevent :( uievent *) event {}// method called when the finger is lifted from the screen-(void) cctouchesended :( nsset *) touches withevent :( uievent *) event {}

This kind of listening method applies to eazy, but it should be noted that the cclayout class is monitored here;

2. listen for distribution. As I said just now, every interface of the game may be a cclayout, but if I want a ccsprite hero to listen separately, in other words, when I want to listen to one of the many genie in cclayout, I need to use the listener distribution method;

Assume that we have customized a class XX to inherit ccsprite, and another YY class also inherits ccsprite, and instances of XX and YY classes exist in a layout, so I want to listen to the XX and YY types separately. First, let the XX inherited ccsprite classes use the <cctargetedtouchdelegate> protocol for the YY classes;

(No self. istouchenabled = yes in ccsprite; Do not write this function directly ~)
 

The Code is as follows:

@interface XX : CCSprite <CCTargetedTouchDelegate>{}

Then, rewrite a function in the current implementation class as follows:

-(void) registerWithTouchDispatcher{[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];}

Alternatively, you can place the registered code in the rewritten onenter function;


(This function registers a listener. If nothing is written in it, no touch screen events will be triggered ;)

Rewrite the touch event functions as follows:

// Listen to the first triggered event-(bool) cctouchbegan :( uitouch *) touch withevent :( uievent *) event {return no;} // listen to the mobile event-(void) cctouchmoved :( uitouch *) touch withevent :( uievent *) event {}// listen for the exit event-(void) cctouchended :( uitouch *) touch withevent :( uievent *) event {}

As you can see, this listener method has a return type-Boolean value in the cctouchbegan function in addition to the various listener functions similar to the first one. The functions are described in detail below; if both XX and YY implement the second listener mode, after the user touch screen (the current user triggers cclayout of XX and YY instances) first, it will enter the cctouchbegan function in XX or yy. If it first enters the XX class, the cctouchbegan in XX class will be responded, if return true; indicates that the user's touch screen message is no longer transmitted to the YY class for response, that is, the user does not respond to the cctouchbegan function in the YY class, if return false; the current touch screen information will be passed to other registered types;

In a word, the return value indicates that the user's touch event has been processed, and the other event will not be listened on again. If it is false, it will be handed over to other registered types for processing;

The second method of listening is more commonly used to facilitate processing. As for registration, it is generally placed in the onenter function. The onenter function is a function that will be responded to during switching between ccscene, the call sequence is as follows:

// When [ccdirereplreplacescene: XX] is used to replace a scenario, the following three methods are called // The call sequence is: // 1. + (ID) scene of othterscene --> // 2. init of otherscene --> // 3. otherscene onenter --> // 4. running transition effect // 5. the onexit function of the current scene --> // 6. onentertransitiondidfinish () of otherlayout // 7. the dealoc function of the current scene-(void) onenter {// call the init method of other scene and then call this method. // If cctransitionscene is used, this method will be called after the transition effect starts. // (if the super onenter is not called, there may be no issue to the touch and accelerator.) [Super onenter];}-(void) onentertransitiondidfinish {// this function will be called after onenter is called // If cctransitionscene is used, this method will be called when the transition effect is completed [Super onentertransitiondidfinish];}-(void) onexit {// this function will be called before dealloc is called; // If cctransitionscene is used, this method will be called after the transition effect ends. // (if super onexit is not called, this scenario may not be released from memory.) [Super onexit];}

After introducing the monitoring event, the most important thing on the touch screen should be multiple contacts;

// ----- Obtain the nsset * alltouches = [event alltouches]; Parameters * touchone = [[alltouches allobjects] objectatindex: 0]; Parameters * touchtwo = [[alltouches allobjects] objectatindex: 1]; //... and so on

It is easy to get multiple points, so we will write down several common judgments below:

1-determine whether the user clicks or double-click (for a contact)

 
 

If ([alltouches count] = 1) {uitouch * touchone = [[alltouches allobjects] objectatindex: 0]; Switch ([touchone tapcount]) {Case 1: // click cclog (@ "% @", @ "click"); break; Case 2: // double-click cclog (@ "% @", @ "double-click "); break ;}}

1-determine whether the user's two contacts are closed or separated (for two contacts)

 
 

 
If ([alltouches count] = 2) {// modify and handle the modification as appropriate. It cannot be obtained at the same time. Otherwise, it must be the same (one can be obtained at the beginning and the other at the end) required * touchone = [[alltouches allobjects] objectatindex: 0]; required * touchtwo = [[alltouches allobjects] objectatindex: 1]; cgfloat * disfirst = [self distance: [touchone locationinview: [self view] todistance: [touchtwo locationinview: [self view]; uitouch * touchone = [[alltouches allobjects] objectatindex: 0]; uitouch * touchtwo = [[alltouches allobjects] objectatindex: 1]; cgfloat * disfinal = [self distance: [touchone locationinview: [self view] todistance: [touchtwo locationinview: [self view]; If (disfirst> disfinal) {cclog (@ "% @", @ "");} else {cclog (@ "% @", @ "separated ");}}

Here I will roughly write it together to determine whether the two contacts are combined. In fact, when the user just clicked the screen, he recorded the distance between the two points as disfirst, and then left the screen (or moved the event) at the two contacts) computing

The distance between the current two contacts is disfinal. The distance between disfirst and disfinal can be used to determine whether to combine or separate them;

(Cclog is a cocos2d packaging printing method. This type of printing will not be compiled into the program during compilation and release of the official game program, but nslog will always exist! Note !)

Finally, two functions are provided to obtain (convert) coordinates in functions monitored in different ways. cocos2d is a framework built by OpenGL, so coordinate conversion is required;

+(CGPoint) locationFromTouches:(NSSet*)touches{return [self locationFromTouch:[touches anyObject]];}+(CGPoint) locationFromTouch:(UITouch*)touch{CGPoint touchLocation = [touch locationInView: [touch view]];return [[CCDirector sharedDirector] convertToGL:touchLocation];}

You can see the difference between the two methods. One is uitouch, the other is nsset, the other is single listener, and the other is distribution listener;

OK. This chapter is here ~~~ (Closed development will take place next week. I am sleeping at the company, coughing, and bringing my Doraemon pants, Wahaha ~)

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.