Cocos2d-x judge sprite click

Source: Internet
Author: User

# Based on cocos2d-x 1.0.1

We often need to determine whether a user's click operation falls on a Sprite, so that the Sprite can respond.

However, if we inherit the ccsprite class to implement our own sprite class, the size of the generated view will be filled with the screen. Multiple sprite instances overlap at the same layer. As a result, the touch point passed through the callback function is relative to the top Sprite.

Fortunately, we can use the ccdirector: shareddirector ()-> converttogl (cctouch * Touch-> locationinview (); method to obtain the absolute coordinates of touch point. Then, you can traverse all the Sprite on the screen and check the position to determine which sprite the user clicks should accept.

But in fact Cocos2d-x has provided us with a very convenient way to determine which sprite the user clicked.

Assume that here we have a mysprite that inherits ccsprite, and in response to the click event, we also need to inherit cctargetedtouchdelegate.
The latter class provides three virtual callback functions:

  virtual   bool  cctouchbegan  ( cctouch  *  touch, ccevent  *  event )  ;  virtual   void  cctouchmove  ( cctouch  *  touch, ccevent  *  event )  ;   virtual   void  cctouchended  ( cctouch  *  touch, ccevent  *  event )  ; 

These three functions are called when the user clicks, clicks, and drags and raises his finger.

The Return Value of the cctouchbegan function is of the bool type, because the system determines whether the click message has been delivered to the sprite required by the user based on the return value of the callback function.

Specifically: If the callback function returns true, it indicatesProgramThe sprite who has considered that the touch message needs to be received has received the touch message. So far, the touch message transmission of the program will be used only at this layer (it will also be used by cctouchmove and cctouchended at this layer ). If false is returned, the touch message is not transmitted to the sprite that needs to receive the message. At this time, the system will pass the touch message to the next Sprite, and the cctouchbegan of this layer is used to process the message.

Post exampleCode:

 Bool Mysprite ::  Cctouchbegan  ( Cocos2d ::  Cctouch   * Touch, cocos2d :: Ccevent   * Event )  {      If  ( Containstouchlocation ( Touch )  )  {          Cout  <  "Touch :"  < This -  > Gettag(  )  < Endl ;          Return   True  ;      }      Return   False  ;  } 

Containstouchlocation is implemented by the user: You can obtain the position in the function and compare it with the touch passed into the function to determine whether the user's touch operation falls within the sprite area.

If it falls into this region, true is returned to interrupt the message chain. At the same time, this sprite will correspond to cctouchmove and cctouchended.
If it does not fall into this region, false is returned and the message is sent to the sprite of the next layer.

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.