【发布时间】:2016-10-15 11:04:14
【问题描述】:
我有几个 cocos 布局,它们是各种面板或菜单。我想知道如何像大多数应用程序一样在他们的区域之外通过触摸输入来关闭。
基本上如何通过点击屏幕上的任何空白区域来关闭弹出菜单。
【问题讨论】:
我有几个 cocos 布局,它们是各种面板或菜单。我想知道如何像大多数应用程序一样在他们的区域之外通过触摸输入来关闭。
基本上如何通过点击屏幕上的任何空白区域来关闭弹出菜单。
【问题讨论】:
基本上你计算屏幕边缘和弹出窗口之间的空间,然后调用析构函数。
我就是这样做的:
在初始化中
auto touchListener = cocos2d::EventListenerTouchOneByOne::create();
touchListener->setSwallowTouches(true);
touchListener->onTouchBegan = CC_CALLBACK_2(CLASS::onTouch, this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener, this);
触摸时
bool CLASS::onTouch(cocos2d::Touch* touch, cocos2d::Event* event)
{
int ySize = visibleSize.height - holder->getContentSize().height;
int locationY = touch->getLocation().y;
if((locationY > 0 && locationY < ySize/2) || (locationY > origin.y + ySize/2 + holder->getContentSize().height && locationY < visibleSize.height))
{
this->removeFromParentAndCleanup(true);
}
return true;
}
【讨论】: