您的位置:首页 > 移动开发 > Cocos引擎

cocos2dx 3.3 cocos studio的交互性问题

2016-04-29 15:19 966 查看
最近在使用cocos studio的时候,发现里面有个选项:交互性,一直没有很明白具体是啥意思。通常这种时候,看源码就对了。

交互性

新建了个cocos studio工程,然后在上面加了个控件,做测试。

勾选 交互性 的时候,对应的ccs文件里,会多一个属性 TouchEnable="True"

于是,第一步就完成了,这个 交互性,应该就是指控件是否支持触摸操作。

源码

知道了 交互性 的意义之后,就是看实现了。

找到源码,cocos-->ui-->UIWidget-->Widget类-->setTouchEnable

void Widget::setTouchEnabled(bool enable)
{
if (enable == _touchEnabled)
{
return;
}
_touchEnabled = enable;
if (_touchEnabled)
{
_touchListener = EventListenerTouchOneByOne::create();
CC_SAFE_RETAIN(_touchListener);
// 默认吞噬触摸,可以调用setSwallowTouches接口取消吞噬
_touchListener->setSwallowTouches(true);
// 触摸事件处理
_touchListener->onTouchBegan = CC_CALLBACK_2(Widget::onTouchBegan, this);
_touchListener->onTouchMoved = CC_CALLBACK_2(Widget::onTouchMoved, this);
_touchListener->onTouchEnded = CC_CALLBACK_2(Widget::onTouchEnded, this);
_touchListener->onTouchCancelled = CC_CALLBACK_2(Widget::onTouchCancelled, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(_touchListener, this);
}
else
{
_eventDispatcher->removeEventListener(_touchListener);
CC_SAFE_RELEASE_NULL(_touchListener);
}
}


然后看onTouchBegan

bool Widget::onTouchBegan(Touch *touch, Event *unusedEvent)
{
_hitted = false;
if (isVisible() && isEnabled() && isAncestorsEnabled() && isAncestorsVisible(this) )
{
_touchBeganPosition = touch->getLocation();
if(hitTest(_touchBeganPosition) && isClippingParentContainsPoint(_touchBeganPosition))
{
_hitted = true;
}
}
if (!_hitted)
{
return false;	// 点击不在区域内,返回false,后续处理也不管
}
setHighlighted(true);

if (_propagateTouchEvents)
{
this->propagateTouchEvent(TouchEventType::BEGAN, this, touch);
}

pushDownEvent();
return true;	// 点击在区域内,返回true,后续处理
}
再看releaseUpEvent

void Widget::releaseUpEvent()
{
this->retain();
if (_touchEventCallback)
{
_touchEventCallback(this, TouchEventType::ENDED);	// 触摸事件
}

if (_touchEventListener && _touchEventSelector)
{
(_touchEventListener->*_touchEventSelector)(this,TOUCH_EVENT_ENDED);
}

if (_clickEventListener) {
_clickEventListener(this);	// click事件
}
this->release();
}


小结一下:
交互性勾选是,默认吞噬触摸,如果点击在控件区域内,则处理后续事件,在区域外,则不处理。

click事件在up时触发。

总结

如果希望控件处理触摸、点击事件,就需要勾选 交互性

如果希望事件往下层传播,那就在代码里找到对应的控件,调用setSwallowTouches(false)


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: