Cocos2dx-3.2 notes-click events

Source: Internet
Author: User

Click Event Detection

Directly Add code

Auto dispatcher = Director: getinstance ()-> Geteventdispatcher (); Auto listener = Eventlistenertouchonebyone: Create (); listener -> Ontouchbegan = cc_callback_2 (gamelayer: ontouchbegan, This  ); Listener -> Ontouchmoved = cc_callback_2 (gamelayer: ontouchmoved, This  ); Listener -> Ontouchended = cc_callback_2 (gamelayer: ontouchended,This  ); Dispatcher -> Addeventlistenerwithscenegraphpriority (listener, This  );  Bool Gamelayer: ontouchbegan (touch * touch, event * Event  ){  Return   True  ;}  Void Gamelayer: ontouchmoved (touch * touch, event * Event  ){}  Void Gamelayer: ontouchended (touch * touch, event * Event  ){} 

 

Sprite Detected

 

BoolGamelayer: checktouchali (touch *Touch ){//Converts the click point to the coordinates in the coordinate system, that is, the origin point relative to alisprite (a sprite ).Vec2 Pt = alisprite->Converttouchtonodespace (touch );IntNw = alisprite->Getrect (). Size. width;IntNH = alisprite->Getrect (). Size. height; rect RC (0,0, NW, NH );ReturnRC. containspoint (PT );}

Sprite and drag

VoidGamelayer: setalipostotouchpos (touch *Touch) {vec2 PT= Alisprite->Converttouchtonodespace (touch );IntNw = alisprite->Getrect (). Size. width;IntNH = alisprite->Getrect (). Size. height; vec2 touchpos= Vec2 (alisprite-> getpositionx ()-NW *0.5+PT. X, alisprite-> Getpositiony ()-NH *0.5+PT. Y); alisprite->Setposition (touchpos );}

 

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.