cocos2d的。如何创建弹出模式对话框(使用CCLayer)

时间:2011-09-29 14:59:44

标签: dialog cocos2d-iphone popup modal-dialog

我需要创建一个模式对话框(继承自CCLayer),它显示在主图层的中心。 在显示时,您无法按对话框外的任何其他按钮。 就像MS Windows系统中的普通模态窗口一样。

问题在于,当对话框显示时,我无法弄清楚如何禁用主层的触摸事件。

任何帮助都会受到重视。

2 个答案:

答案 0 :(得分:1)

只需设置

self.isTouchEnabled = NO;
显示弹出窗口时在主层中

。然后将其设置回YES。

答案 1 :(得分:0)

这适用于Cocos2d-x,但也适用于Cocos2d。

我已将setParent更改为:

virtual void setParent(CCNode* parent)
{
    CCLayerColor::setParent(parent);        

    if (parent)
    {
        saveTouchesRecursive(parent);

        for (unsigned int i=0; i<mSavedTouches.size(); i++)
        {
            CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(mSavedTouches[i]);
        }
    }
    else
    {
        for (unsigned int i=0; i<mSavedTouches.size(); i++)
        {
            CCStandardTouchDelegate* standardTouchDelegate =  dynamic_cast<CCStandardTouchDelegate*>(mSavedTouches[i]);
            CCTargetedTouchDelegate* targetedTouchDelegate =  dynamic_cast<CCTargetedTouchDelegate*>(mSavedTouches[i]);

            CCLayer* layer =  dynamic_cast<CCLayer*>(mSavedTouches[i]);

            if (layer)
            {
                layer->registerWithTouchDispatcher();
            }
            else if (standardTouchDelegate)
            {
                CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(mSavedTouches[i], 0);
            }
            else if (targetedTouchDelegate)
            {
                CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(mSavedTouches[i], 0,  false);
            }
            else
            {
                CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(mSavedTouches[i], 0,  false);
            }
        }
    }
}

并添加了以下递归代码以保存触摸代理,以便在对话框解除时恢复它们:

std::vector<CCTouchDelegate*> mSavedTouches;
void saveTouchesRecursive(CCNode* node)
{
    if (node != this)
    {    
        CCTouchDelegate* touchDelegate = dynamic_cast<CCTouchDelegate*>(node);
        if (touchDelegate)
        {
            CCTouchHandler* handler = CCDirector::sharedDirector()->getTouchDispatcher()->findHandler(touchDelegate);

            if (handler)
            {
                mSavedTouches.push_back(touchDelegate);
             }
        }

        for (unsigned int i=0; i<node->getChildrenCount(); i++)
        {
            CCNode* childNode = dynamic_cast<CCNode*>(node->getChildren()->objectAtIndex(i));

            if (childNode)
            {
                saveTouchesRecursive(childNode);
            }
        } 
    }
}