点击拖动按钮后,想让所在的layer监听到屏幕的触摸事件,可以把该按钮拖动开始的时候设置setTouchEnabled为false;
例:
#include "ui/UIButton.h" bool myDemo::init() { // 设置点击事件 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 ); // 绑定 getEventDispatcher()->addEventListenerWithSceneGraphPriority( listener, this ); // 按钮创建 Button *btn = ***; btn ->addTouchEventListener( CC_CALLBACK_2( myDemo::BtnCallBack, this ) ); this->addChild( btn ); return true; } void myDemo::BtnCallBack( Ref *p, cocos2d::ui::Widget::TouchEventType type ) { Button *btn = (Button *)p; // if ( type == Widget::TouchEventType::BEGAN ) { btn->setTouchEnabled( false ); } } bool myDemo::onTouchBegan( cocos2d::Touch *touch, cocos2d::Event *event ) { return true; } void myDemo::onTouchMoved( cocos2d::Touch *touch, cocos2d::Event *event ) { } void myDemo::onTouchEnded( cocos2d::Touch *touch, cocos2d::Event *event ) { }
//当拖动按钮时,会进入onTouchMoved方法