Cocos2d-x: Solve the problem of touching the touch event on the layer where you clicked the drag button

Source: Internet
Author: User

Click the drag button, want to let the layer monitor hear the screen touch event, you can drag the button to start setting settouchenabled to false;
Cases:

#include "Ui/uibutton.h"

BOOL Mydemo::init () {

  

Set Up Click events
Eventlistenertouchonebyone *listener = Eventlistenertouchonebyone::create ();
Listener->ontouchbegan = Cc_callback_2 (Mydemo::ontouchbegan, this);
listener->ontouchmoved = Cc_callback_2 (mydemo::ontouchmoved, this);
listener->ontouchended = Cc_callback_2 (mydemo::ontouchended, this);


Binding
Geteventdispatcher ()->addeventlistenerwithscenegraphpriority (listener, this);

  

Button creation
Button *BTN = * * *;
BTN->addtoucheventlistener (Cc_callback_2 (Mydemo::btncallback, this));

This->addchild (BTN);

}

void Mydemo::btncallback (Ref *p, Cocos2d::ui::widget::toucheventtype type) {

Button *BTN = (button *) p; //

if (type = = Widget::toucheventtype::began) {

Btn->setposition (VEC2 (Btn->getpositionx ()-btn->getpositiony () + 15));
Btn->settouchenabled (FALSE);

}

}

BOOL Mydemo::ontouchbegan (Cocos2d::touch *touch, cocos2d::event *event) {

return true;

}

void Mydemo::ontouchmoved (Cocos2d::touch *touch, cocos2d::event *event) {

Cclog ("-------------------%d--------------", a++);

}

void mydemo::ontouchended (Cocos2d::touch *touch, cocos2d::event *event) {


}

When you drag a button, you enter the Ontouchmoved method

Cocos2d-x: Solve the problem of touching the touch event on the layer where you clicked the drag button

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.