Cocos2d-x development instance: single touch event

Source: Internet
Author: User

Cocos2d-x mobile games IOS iOS 8 games

Next, let's take a look at the implementation process of single point of touch events in the layer through an example. Feel its shortcomings and advantages. Shows the instance scenario. There are two cube genie in the scenario. You can click and move them.


The specific implementation code of helloworldscene. cpp is as follows:

Bool helloworld: Init () {If (! Layer: Init () {returnfalse ;}...... settouchenabled (true); // set it to single-touch settouchmode (touch: dispatchmode: one_by_one); returntrue;} bool helloworld: ontouchbegan (touch * touch, * event) ① {log ("ontouchbegan"); // get the boxa genie autoboxa = This-> getchildbytag (kboxa_tag) by TAG ); ② // If the boxa genie is clicked if (this-> ISTAP (boxa, touch) ③ {log ("boxasprite tap"); boxa-> runaction (scaleby :: create (0.06, 1.06); ④ retur Ntrue; ⑤} // get the boxb genie autoboxb = This-> getchildbytag (kboxb_tag) through tag ); ⑥ // If the boxb genie is clicked if (this-> ISTAP (boxb, touch) {log ("boxbsprite tap"); boxb-> runaction (scaleby :: create (0.06, 1.06); returntrue;} 7returnfalse;} void helloworld: ontouchmoved (touch * touch, event * event) Success {log ("ontouchmoved "); // obtain the boxa genie autoboxa = This-> getchildbytag (kboxa_tag) through tag; // If the boxa genie is clicked if (this-> ISTAP (boxa, Uch) {log ("boxasprite tap"); // move the coordinates of the current button genie boxa-> setposition (boxa-> getposition () + touch-> getdelta (); return;} // get the boxb genie autoboxb = This-> getchildbytag (kboxb_tag) through tag ); // If the boxb genie is clicked if (this-> ISTAP (boxb, touch) {log ("boxbsprite tap "); // move the Coordinate Position of the current button genie boxb-> setposition (boxb-> getposition () + touch-> getdelta (); Return ;}} void helloworld :: ontouchended (touch * touch, event * event) Events {log ("On Touchended "); // obtain the boxa genie autoboxa = This-> getchildbytag (kboxa_tag) through tag; // If the boxa genie is clicked if (this-> ISTAP (boxa, touch) {log ("boxasprite tap"); boxa-> runaction (scaleto: Create (0.06, 1.0); Return ;}// tag (TAG) obtain the boxb genie autoboxb = This-> getchildbytag (kboxb_tag); // If the boxb genie is clicked if (this-> ISTAP (boxb, touch )) {log ("boxbsprite tap"); boxb-> runaction (scaleto: Create (0.06, 1.0); Return ;}} bool helloworld: ISTAP (node * node, touch * Touch) ready {// obtain the coordinates of the touch point relative to the node pointlocationinnode = node-> converttonodespace (touch-> getlocation ());? Sizes = node-> getcontentsize ();? Rectrect = rect (0, 0, S. Width, S. Height );? // Click the range to determine if (rect. containspoint (locationinnode ))? {Returntrue;} returnfalse ;}

The above Code defines the Three touch event functions in line ①, between, and between. The touch parameter of the function is the touch point in the layer, and the event is the touch event, we cannot use the auto target = static_cast <sprite *> (Event-> getcurrenttarget () Statement in section 8.1.3 to get the genie to be clicked. In fact, event-> getcurrenttarget () the function obtains the event source, where the event source is the current layer rather than the sprite object. So how can we determine which genie has been clicked? My solution is to judge each genie one by one. Therefore, we are ~ The fifth line of code determines whether the genie boxa is clicked ~ 7. The line of code determines whether the genie boxb is clicked.

The ISTAP function is used in line ③ of the code. We define this function in the nth line, which is used to determine whether the touch point is in the genie, this judgment is mainly determined by the containspoint function of rect. In the function? Get the local coordinates of the touch point and the sprite object using the line code. Number? Line code is used to obtain the size of the sprite object. Number? The line code creates a rect variable by the size of the sprite object. Number? Line Code rect. containspoint (locationinnode) is used to determine whether the touch point is within the scope of the sprite object.




More content please pay attention to the Cocos2d-x series of books "Cocos2d-x practice (Volume I): c ++ development" book exchange discussion site: http://www.c Ocoagame.netWelcome to cocos2d-x Technology Discussion Group: 257760386, 327403678


Cocos2d-x development instance: single touch event

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.