寻找处理多个触摸事件的游戏的模式建议,代码变得混乱

时间:2014-08-07 06:21:00

标签: design-patterns

我的手机游戏中包含处理用户触摸的代码部分 基本上它的剂量是:  1.触摸的主要功能
 2.在模块A中循环所有精灵检查触摸坐标是否在精灵之一的边界内如果是与操作相关联的调用函数则断开循环
 3.与模块B的2相同  4.与2,3相同,带模块C等  5.在循环的某些部分,有布尔指示符表示:
 6.如果boolInd1为真,则忽略模块A中的触摸  7.如果boolInd2为真,则忽略模块B的触摸 等等......它很长的功能 如果我可以将此功能禁用为结构化模式,我就会徘徊?

这里的功能只是为了展示难看的复杂性:

void MainGame::onTouchesBegan(const std::vector<Touch*>& touches, Event  *event)
{

    bool FontSignTouched = false; 
    for( auto& touch : touches)
    {
        auto location = touch->getLocation();
         //once the next level button is triggered to show all other touchs are disabled 

        Vector<Node *> ScoreContainerChildren = pScoreContainer->getChildren();
        for (auto iter = ScoreContainerChildren.begin(); iter != ScoreContainerChildren.end(); ++iter) 
        {
            Node *childNode = *iter; 
            if(childNode->getTag() == buttons_tags::SOUND_BT)
            {

                Point thisTouchScoreContainer = pScoreContainer->convertTouchToNodeSpace(touch); 
                Sprite* pSoundBT = static_cast<Sprite*>(*iter); 
                if(pSoundBT->getBoundingBox().containsPoint(thisTouchScoreContainer))
                {

                    pScoreContainer->setSoundButtonSpriteFrame(true);
                    break;
                }

            }

            if(childNode->getTag() == buttons_tags::POINTS_CONTAINER_NODE)
            {

                Sprite* pCoinsBT = (Sprite*)childNode->getChildByTag(buttons_tags::COINS_IMG_BT);
                Sprite* pCoinsCountFrameBT = (Sprite*)childNode->getChildByTag(buttons_tags::COINS_COUNT_FRAME);
                Point thisTouchPointsContainerNode = childNode->convertTouchToNodeSpace(touch); 

                if(pCoinsBT->getBoundingBox().containsPoint(thisTouchPointsContainerNode))
                {

                    setPopUpWindow();
                    break;
                }


                if(pCoinsCountFrameBT->getBoundingBox().containsPoint(thisTouchPointsContainerNode))
                {

                    setPopUpWindow();
                    break;
                }                
            }

        }
        Vector<Node *> FontSelectionContainerChildren = pFontSelectionContainer->getChildren();
        for (auto iter = FontSelectionContainerChildren.begin(); iter != FontSelectionContainerChildren.end(); ++iter) 
        {
            Node *childNode = *iter; 
            if(childNode->getTag() == sign_tags::LETTER_SIGH)
            {

                Point thisTouchPositionFontSelection = this->convertTouchToNodeSpace(touch); 
                Sign* pSign = static_cast<Sign*>(*iter); 
                if(pSign->getBoundingBox().containsPoint(thisTouchPositionFontSelection))
                {

                    Settings::getInstance()->getSoundManager().playEffect(FONT_TO_SOLUTION);
                    pSolutionContainer->setFontSelectionToSulotionFont(pSign);
                    FontSignTouched = true;
                    break;
                }
            }
        }

        Vector<Node *> thisSelectionChildren = this->getChildren(); 
        if(!FontSignTouched)
        {
            for (auto iter = thisSelectionChildren.begin(); iter != thisSelectionChildren.end(); ++iter) 
            {
                Node *childNode = *iter; 
                if(childNode->getTag() == sign_tags::LETTER_SIGH)
                {
                    Point thisTouchPositionFontSelection = this->convertTouchToNodeSpace(touch);  
                    Sign* pSign = static_cast<Sign*>(*iter); 
                    if(pSign->getBoundingBox().containsPoint(thisTouchPositionFontSelection))
                    {

                        Settings::getInstance()->getSoundManager().playEffect(SOLUTION_TO_FONT);
                        pFontSelectionContainer->removeFromMainParantAndSetInSprite(pSign);
                        break;
                    }

                }
                if(childNode->getTag() == buttons_tags::NEXT_BT)
                {
                    Point thisTouchPositionFontSelection = this->convertTouchToNodeSpace(touch);  
                    Sign* pSign = static_cast<Sign*>(*iter); 
                    if(pSign->getBoundingBox().containsPoint(thisTouchPositionFontSelection))
                    {

                    }
                }
            }
        }
        else
        {
            FontSignTouched = false;
        }
    }

} 

0 个答案:

没有答案