[IOS-Cocos2d Game Development 5] multi-contact and touch screen event details (single monitor, event distribution)

Source: Internet
Author: User

Li huaming himi original, reprint must note in the obvious: Reprinted from [Black Rice gamedev block] original article link: Success! -------------------------------- This article provides a detailed description of Multi-contact and touch-screen events, but it is ignored that the multi-contact support is enabled! The procedure is as follows: first enter appdelegate. in the M class, [CPP] view plaincopy <strong>-(void) applicationdidfinishlaunching :( uiapplication *) application {}</strong> in the above method, add the following code to enable multi-Contact Support: [CPP] view plaincopy <strong> // supports multi-contact [viewcontroller. view setmultipletouchenabled: Yes]; </strong> set multiple contacts for other classes: [[ccdirector shareddirector] openglview] setmultipletouchenabled: Yes]; ------------------------ added in this Chapter ----------------------------------- ----------------------------------------- Cocos2d has been being gnushed in recent days and many things have been digested. You can basically grasp the game of a handwriting company. Today, I will share some important experiences with you, for new kids shoes as a reference; this article will introduce cocos2d's analysis of user touch screen listening events (cocos2d has many detailed articles and tutorials, I am here for my own understanding.) To get started: cocos2d monitors touch-screen events in two ways: 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 listening first: [CPP] view plaincopyself. istouchenabled = ye S; then rewrite the listening function: [CPP] view plaincopy // 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 {} // The method called when the finger is lifted from the screen-(void) cctouchesended :( nsset *) touches withevent :( uievent *) event {} this kind of listening hacker eazy, note that the cclayout class is listened to 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 monitor it separately Listen, or when there are many genie in cclayout I want to listen to one of them separately, I need to use the listener distribution form at this time; 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 class. (No self in ccsprite. istouchenabled = yes; Do not write this function directly ~) The Code is as follows: [CPP] view plaincopy @ interface XX: ccsprite <cctargetedtouchdelegate >{}. Then, rewrite a function in the current implementation class as follows: [CPP] view plaincopy-(void) registerwithtouchdispatcher {[[cctouchdispatcher shareddispatcher] addtargeteddelegate: Self priority: 0 swallowstouches: Yes];} or 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 generated.) rewrite the event functions of touch, as shown below: [CPP] view plaincopy // listens for the first trigger event-(bool) cctouchbegan :( uitouch *) touch Withevent :( uievent *) event {return no;} // listen to the mobile event-(void) cctouchmoved :( uitouch *) touch withevent :( uievent *) event {} // listen to the exit event-(void) cctouchended :( uitouch *) touch withevent :( uievent *) event {} You can see, in addition to a variety of listening functions, this method has a return type-Boolean value in the cctouchbegan function. 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, the cctouchbegan function in XX or YY is entered. Assume that the cctouchbegan function in XX class is first entered. 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 no longer responds to the cctouchbegan function in the YY class, if return false; the current touch screen information will be transmitted to other registered types. In a word, the return value indicates that the user's touch event has been processed, and the other will not be listened to again; if it is false, it will be handed over to other registered types for processing; then the second method of listening is more commonly used to facilitate processing, so as to register, generally, it is placed in the onenter function. The onenter function is the function that will be responded to when switching between ccscene, which is equivalent to the lifecycle function of ccscene. The Calling sequence is as follows: [CPP] view plaincopy // use [ccdirector replacescene: XX]. When replacing a scenario, the following three methods are called // The call sequence is: // 1. + (ID) scene of othterscene --> // 2. other Init of scene --> // 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 complete [Super onentertransiti Ondidfinish];}-(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, the current scenario may not be released from the memory) [Super onexit];} after introducing the listener event, the most important thing in the touch screen is multi-touch. [CPP] view plaincopy // ----- obtain multi-touch nsset * alltouches = [event alltouches]; uitouch * touchone = [[alltouches allobjects] objectatindex: 0]; uitouch * touchtwo = [[alltouches allobjects] objectatindex: 1]; //... it's easy to get more points by analogy. Write down several commonly used judgments: 1-determine whether the user clicks or double-clicks (for a contact) [CPP] view plaincopy </PRE> <p> </P> <PRE name = "code" class = "CPP"> 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) [CPP] view plaincopy </PRE> <p> </P> <P class = "p1"> </P> <PRE name = "code" clas S = "CPP"> <PRE name = "code" class = "CPP"> If ([alltouches count] = 2) {// modify the statement as appropriate and cannot be retrieved at the same time, otherwise, it must be the same (one can be at began and 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 * touchtwo = [[alltouches allobjects] objectatindex: 1]; cgfloat * disfinal = [self distance: [touchone locationinview: [self view] todistance: [touchtwo locationinview: [self view]; If (disfirst> disfinal) {cclog (@ "% @", @ "");} else {cclog (@ "% @", @ "separate") ;}} here I will roughly write it together, to determine whether the two contacts are closed is to record the distance between the two points on the touch screen as disfirst, and then leave the two contacts on the screen (or move the event) when calculating the distance between the current two contacts disfinal, then finally according to the distance between disfirst and disfinal (Cclog is a cocos2d packaging printing method, which 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; [CPP] view plaincopy + (cgpoint) locationfromtouches :( nsset *) touches {return [self locationfromtouch: [touches anyobject];} + (cgpoint) locationfromtouch :( uitouch *) touch {cgpoint touchlocation = [Touch locationinview: [Touch view]; return [ccdirector shareddirector] converttogl: touchlocation];} the two methods show the difference at first glance, one is uitouch, the other is nsset, and the other is Is a single listener, one is a 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 ~)
The main problem to be discussed is the use of registration listening (event allocation to listen to touch screens). As mentioned in the previous chapter, you must first register this method: the following code: [CPP] view plaincopy // register an independent touch event [[cctouchdispatcher shareddispatcher] addtargeteddelegate: Self priority: 0 swallowstouches: Yes]; if multiple registration listening events exist, you can set this parameter based on the priority parameter. The higher the value of this parameter, the lower the priority. Assume that XX and YY have registered a listener. If XX has a higher priority than YY, first, the XX listener function will be enabled. If the above method is used for listening, the following events will be monitored: [CPP] view plaincopy-(bool) cctouchbegan :( uitouch *) touch withevent :( uievent *) event {return no;}-(void) cctouchmoved :( uitouch *) touch withevent :( uievent *) event {}-(void) cctouchended :( uitouch *) touch withevent :( uievent *) event {} If yes is returned in the cctouchbegan function in XX, the listener function in another listener (yy) is not responded; this topic is introduced in the previous chapter. However, if you want both XX and YY to listen to events, you can return no in cctouchbegan, however, if you want to process the cctouchmoved mobile event function in YY or XX, you will find that neither XX nor YY will respond to its function, because when you retrun no; although the Code between cctouchbegan and return is executed, you actually tell cocos2d to discard the event processing, let cocos2d continue to allocate touch-screen events to other registered classes for listening processing until the end or cocos2d gets return true; then the solution is as follows: for example, if you want to listen to the cctouchmoved event in the YY class, in order not to affect the listening of the cctouchbegan event in XX, you should set the priority of the XX class to be higher than that of the YY class when registering the listener, and in XX class, the cctouchbegan function return no. After the user touch screen, the user first enters XX class to process the event, and then enters the YY class (because XX class return no, in the cctouchbegan function of the yy class, return yes; so that cocos2d can respond to the cctouchmoved function. If you want the XX and YY classes to respond to the cctouchmoved function, at least I cannot use this event allocation method. I don't know if it is a bug left by cocos2d version 1.0 or another cause. I will share it with you today;

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.