"Ios-cocos2d game development Five" "2" multiple contacts and touch screen event detailed (single monitor, event distribution)

Source: Internet
Author: User

Then probably introduced after the monitoring event, then the touch screen of the most concerned should be more contact;

-----get multi-touch nsset *alltouches = [Event Alltouches];    Uitouch *touchone = [[Alltouches allobjects]objectatindex:0];    Uitouch *touchtwo = [[Alltouches allobjects]objectatindex:1]; //... Analogy

Get more ruthless simple, then the basic commonly used to write down the following: 1-to determine whether the user clicks or double click (for a contact point)

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;        }   }   

2-Determine whether the user's two contacts are closed or separate (for two contacts).

if ([alltouches count]==2)  {         //properly modified for processing, can not be taken at the same time, Otherwise it must be the same (can be one in the began  one at end)         uitouch *touchone = [ [alltouches allobjects]objectatindex:0];        uitouch *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&NBsp;*touchtwo = [[alltouches allobjects]objectatindex:1];         cgfloat *disfinal  =[self distance:[touchone locationinview:[self view]]                                  todistance:[touchTwo  locationinview:[self view]]];        if  (disfirst>disfinal)  {            cclog (@ "%@", @ "closure");        }else{            cclog (@ "%@", @ " Separate ");        }   }   

Here I write roughly together, to determine whether two contacts are closed is actually the user just touch the distance between two points of the record to do Disfirst, and then two contacts left the screen (or moving events) when the calculation

The distance between the current two contacts is disfinal, then it is possible to know whether to close or separate according to the distance between Disfirst and disfinal;

(Cclog is the cocos2d package printing method that is not compiled into a program when compiling a formal game program, but NSLog will always exist.) Be aware. )

Finally, two functions are given to compute the coordinates of the functions in different modes of listening, because cocos2d is the framework of OpenGL, so coordinate transformation is needed.

+ (Cgpoint) Locationfromtouches: (nsset*) touches {return [self locationfromtouch:[touches anyobject]]; } + (Cgpoint) Locationfromtouch: (uitouch*) Touch {cgpoint touchlocation = [Touch locationinview: [Touch view]]        ;    return [[Ccdirector Shareddirector] converttogl:touchlocation]; }

Two methods can see the difference, one is Uitouch, one is Nsset, one is a single monitor, one is the distribution of listening;


Related Article

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.