Cocos:如何检测 area/layout 内的触摸输入
Cocos: How to detect touch input inside of a area/layout
我有几个 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;
}
我有几个 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;
}