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