【问题标题】:Cocos: How to detect touch input inside of a area/layoutCocos:如何检测区域/布局内的触摸输入
【发布时间】:2016-10-15 11:04:14
【问题描述】:

我有几个 cocos 布局,它们是各种面板或菜单。我想知道如何像大多数应用程序一样在他们的区域之外通过触摸输入来关闭。

基本上如何通过点击屏幕上的任何空白区域来关闭弹出菜单。

【问题讨论】:

    标签: c++ cocos2d-x


    【解决方案1】:

    基本上你计算屏幕边缘和弹出窗口之间的空间,然后调用析构函数。

    我就是这样做的:

    在初始化中

    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;
    }
    

    【讨论】:

    • 谢谢! :) 真的很感激。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    • 2016-01-12
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多