如图所示。a是一个widget。然后我为a注册了一个监听器。但是当我点击a以为的区域。事件还是会触发。。不大明白额。。求各位大大讲解
如图所示。a是一个widget。然后我为a注册了一个监听器。但是当我点击a以为的区域。事件还是会触发。。不大明白额。。求各位大大讲解
点击a以外的区域
上边打错了。。
不会的。
你用这段代码重试一下。在HelloWorld项目中测试。
auto widget = Widget::create();
widget->setContentSize(Size(100,100));
widget->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2));
widget->setTouchEnabled(true);
this->addChild(widget);
widget->addTouchEventListener(](Ref *pSender, Widget::TouchEventType type){
switch (type)
{
case Widget::TouchEventType::BEGAN:
CCLOG("BEGAN");
break;
case Widget::TouchEventType::MOVED:
CCLOG("MOVED");
break;
case Widget::TouchEventType::ENDED:
CCLOG("ENDED");
break;
case Widget::TouchEventType::CANCELED:
CCLOG("CANCELED");
break;
default:
break;
}
});
```
谢谢版主。但是3.0之后的事件注册不是用_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,w)这个东西么。addtoucheventlistener已经弃用的啊。
不是的。
弃用的是 CC_DEPRECATED_ATTRIBUTE void addTouchEventListener(Ref* target,SEL_TouchEvent selector);
不是 void addTouchEventListener(const ccWidgetTouchCallback& callback);
后者推荐使用。
addEventListenerWithSceneGraphPriority是通用做法,所以是全屏的。就像是给Sprite注册监听,你必须要手动判断触摸点。
原来如此。还有一个。你给的函数 貌似获取补刀触摸点啊。
把传入的target强转为widget。
然后调下面的接口:
/*
* Gets the touch began point of widget when widget is selected.
*
* @return the touch began point.
*/
CC_DEPRECATED_ATTRIBUTE const Vec2& getTouchStartPos()const{return this->getTouchBeganPosition();}
const Vec2& getTouchBeganPosition()const;
/*
* Gets the touch move point of widget when widget is selected.
*
* @return the touch move point.
*/
CC_DEPRECATED_ATTRIBUTE const Vec2& getTouchMovePos()const{ return this->getTouchMovePosition();}
const Vec2& getTouchMovePosition()const;
/*
* Gets the touch end point of widget when widget is selected.
*
* @return the touch end point.
*/
CC_DEPRECATED_ATTRIBUTE const Vec2& getTouchEndPos()const{return this->getTouchEndPosition();}
const Vec2& getTouchEndPosition()const;
```