示例#1
0
void CCMenuItemToggle::setSelectedIndex(unsigned int index)
{
    if( index != m_uSelectedIndex && m_pSubItems->count() > 0 )
    {
        m_uSelectedIndex = index;
        CCMenuItem *currentItem = (CCMenuItem*)getChildByTag(kCurrentItem);
        if( currentItem )
        {
            currentItem->removeFromParentAndCleanup(false);
        }

        CCMenuItem* item = (CCMenuItem*)m_pSubItems->objectAtIndex(m_uSelectedIndex);
        this->addChild(item, 0, kCurrentItem);
        CCSize s = item->getContentSize();
        this->setContentSize(s);
        item->setPosition( ccp( s.width/2, s.height/2 ) );
    }
}
示例#2
0
void RPGBattleMenu::onMenu(cocos2d::CCObject *pObject)
{
    CCMenuItem *menuItem = (CCMenuItem*)pObject;
    SimpleAudioEngine::sharedEngine()->playEffect("audio_effect_btn.wav");

    switch (menuItem->getTag())
    {
        case kRPGBattleMenuTagSkill:
        {
            CCLog("技能");
            
            this->hideMenu();
            
            this->m_selectedMenuTag = kRPGBattleMenuTagSkill;
            
            CCMenuItemSprite *menuCancel = CCMenuItemSprite::create(CCSprite::createWithSpriteFrameName("commons_btn_back_04.png"), CCSprite::createWithSpriteFrameName("commons_btn_back_04.png"), this, menu_selector(RPGBattleMenu::onMenu));
            menuCancel->setPosition(ccp(43, 596));
            menuCancel->setTag(kRPGBattleMenuTagCancel);
            menuCancel->setScale(0.75);
            this->addChild(menuCancel);
            
            CCTMXTiledMap *selectLayer = CCTMXTiledMap::create(CCString::createWithFormat("battle_select_%s.tmx", CCUserDefault::sharedUserDefault()->getStringForKey(GAME_STYLE).c_str())->getCString());
            selectLayer->setPosition(ccp((CCDirector::sharedDirector()->getWinSize().width - selectLayer->getContentSize().width) / 2, (CCDirector::sharedDirector()->getWinSize().height - selectLayer->getContentSize().height) / 2));
            selectLayer->setTag(kRPGBattleSceneLayerTagSkillSelectDialog);
            
            ((RPGBattleSceneLayer*)this->m_parentNode)->addChild(selectLayer);            
            ((RPGBattleSceneLayer*)this->m_parentNode)->enabledTouched(true);
            
            //显示title和分隔线
            addLab(selectLayer, 199, (CCString*)this->m_stringList->objectForKey("skill_title"), 25, ccp(310, 285));
            CCLabelTTF *titleLab = (CCLabelTTF*)selectLayer->getChildByTag(199);
            titleLab->setFontFillColor(ccc3(144, 144, 144));
            
            CCSprite *separate = CCSprite::createWithSpriteFrameName("separate.png");
            separate->setPosition(ccp(selectLayer->getContentSize().width / 2, 260));
            separate->setScaleX(0.65);
            separate->setTag(198);
            selectLayer->addChild(separate);
            
            //加载技能数据
            CCTableView *tableView = (CCTableView*)selectLayer->getChildByTag(197);
            if(!tableView)
            {
                tableView = CCTableView::create(this, ccp(selectLayer->getContentSize().width, selectLayer->getContentSize().height - 80));
                tableView->setDirection(kCCScrollViewDirectionVertical);
                tableView->setPosition(CCSizeZero);
                tableView->setDelegate(this);
                tableView->setVerticalFillOrder(kCCTableViewFillTopDown);
                tableView->setTag(197);
                selectLayer->addChild(tableView);
                
            }
            
            this->m_tableItems->removeAllObjects();

            string wq = "";
            JsonBox::Value json;
            json.loadFromString(this->m_playerData->m_skill.c_str());
            JsonBox::Array jsonArr = json.getArray();
            for (int i = 0; i < jsonArr.size(); i++)
            {
                char* str = (char*)malloc(10 * sizeof(char));
                OzgCCUtility::itoa(jsonArr[i].getInt(), str);
                wq.append(str);
                
                if(i + 1 < jsonArr.size())
                    wq.append(", ");
                
                free(str);
            }
            if((int)wq.length() > 0)
            {
                CppSQLite3Query query = this->m_db->execQuery(CCString::createWithFormat(SKILL_QUERY, wq.c_str())->getCString());
                while(!query.eof())
                {
                    RPGSkillBtnData *skill = RPGSkillBtnData::create();
                    skill->m_dataId = query.getIntField("id");
                    skill->m_name = query.getStringField("name_cns");
                    skill->m_MP = query.getIntField("mp");
                    skill->m_skillAttack = query.getIntField("skill_attack");
                    skill->m_type = query.getIntField("type");
                    skill->m_attr = query.getIntField("attr");
                    skill->m_enabled = true;
                    
                    //不能使用技能的情况
                    if(this->m_playerData->m_MP <= 0 || this->m_playerData->m_HP <= 0)
                        skill->m_enabled = false;
                    else if(this->m_playerData->m_MP < skill->m_MP)
                        skill->m_enabled = false;
                    
                    this->m_tableItems->addObject(skill);
                    
                    query.nextRow();
                }
                query.finalize();
            }
            
            tableView->reloadData();
            //加载技能数据 end
            
            ((RPGBattleSceneLayer*)this->m_parentNode)->enabledTouched(false);
        }
            break;
        case kRPGBattleMenuTagItems:
        {
            CCLog("道具");
            
            this->hideMenu();
            
            this->m_selectedMenuTag = kRPGBattleMenuTagItems;
            
            CCMenuItemSprite *menuCancel = CCMenuItemSprite::create(CCSprite::createWithSpriteFrameName("commons_btn_back_04.png"), CCSprite::createWithSpriteFrameName("commons_btn_back_04.png"), this, menu_selector(RPGBattleMenu::onMenu));
            menuCancel->setPosition(ccp(43, 596));
            menuCancel->setTag(kRPGBattleMenuTagCancel);
            menuCancel->setScale(0.75);
            this->addChild(menuCancel);
            
            CCTMXTiledMap *selectLayer = CCTMXTiledMap::create(CCString::createWithFormat("battle_select_%s.tmx", CCUserDefault::sharedUserDefault()->getStringForKey(GAME_STYLE).c_str())->getCString());
            selectLayer->setPosition(ccp((CCDirector::sharedDirector()->getWinSize().width - selectLayer->getContentSize().width) / 2, (CCDirector::sharedDirector()->getWinSize().height - selectLayer->getContentSize().height) / 2));
            selectLayer->setTag(kRPGBattleSceneLayerTagItemsSelectDialog);
            
            ((RPGBattleSceneLayer*)this->m_parentNode)->addChild(selectLayer);
            ((RPGBattleSceneLayer*)this->m_parentNode)->enabledTouched(true);
            
            //显示title和分隔线
            addLab(selectLayer, 199, (CCString*)this->m_stringList->objectForKey("items_title"), 25, ccp(310, 285));
            CCLabelTTF *titleLab = (CCLabelTTF*)selectLayer->getChildByTag(199);
            titleLab->setFontFillColor(ccc3(144, 144, 144));
            
            CCSprite *separate = CCSprite::createWithSpriteFrameName("separate.png");
            separate->setPosition(ccp(selectLayer->getContentSize().width / 2, 260));
            separate->setScaleX(0.65);
            separate->setTag(198);
            selectLayer->addChild(separate);
            
            //加载道具数据
            CCTableView *tableView = (CCTableView*)selectLayer->getChildByTag(197);
            if(!tableView)
            {
                tableView = CCTableView::create(this, ccp(selectLayer->getContentSize().width, selectLayer->getContentSize().height - 80));
                tableView->setDirection(kCCScrollViewDirectionVertical);
                tableView->setPosition(CCSizeZero);
                tableView->setDelegate(this);
                tableView->setVerticalFillOrder(kCCTableViewFillTopDown);
                tableView->setTag(197);
                selectLayer->addChild(tableView);
            }
            
            this->m_tableItems->removeAllObjects();
            
            CCArray *existingItems = ((RPGBattleSceneLayer*)this->m_parentNode)->m_existingItems;
            
            for (int i = 0; i < existingItems->count(); i++)
                this->m_tableItems->addObject(existingItems->objectAtIndex(i));
            
            tableView->reloadData();
            //加载道具数据 end
            
            ((RPGBattleSceneLayer*)this->m_parentNode)->enabledTouched(false);
        }
            break;
        case kRPGBattleMenuTagEscape:
        {
//            CCLog("逃跑");
            
            float success = CCRANDOM_0_1();
            if(success >= 0.0 && success <= 0.5)
            {
//                CCLog("逃跑成功");
                
                this->setEnabled(false);
                this->removeFromParentAndCleanup(true);
                
                ((RPGBattleSceneLayer*)this->m_parentNode)->goToMap();
            }
            else
            {
                ((RPGBattleSceneLayer*)this->m_parentNode)->showMsg((CCString*)this->m_stringList->objectForKey("escape_fail"));
                
                //重置进度条
                this->m_playerData->m_progress = 0.0;
                CCProgressTimer *battleProgress = (CCProgressTimer*)this->m_parentNode->getChildByTag(kRPGBattleSceneLayerTagPlayerProgress + this->m_playerData->m_dataId);
                battleProgress->setPercentage(this->m_playerData->m_progress);
                
                ((RPGBattleSceneLayer*)this->m_parentNode)->scheduleUpdate();
                
                CCTMXTiledMap *bgLayer = (CCTMXTiledMap*)this->m_parentNode->getChildByTag(kRPGBattleMenuTagBg);
                bgLayer->removeFromParentAndCleanup(true);
                this->removeFromParentAndCleanup(true);
            }
            
        }
            break;
        case kRPGBattleMenuTagCancel:
        {
//            CCLog("取消");
            this->showMenu();
            
            CCMenuItem *menuCancel = (CCMenuItem*)this->getChildByTag(kRPGBattleMenuTagCancel);
            menuCancel->removeFromParentAndCleanup(true);
            
            switch (this->m_selectedMenuTag)
            {
                case kRPGBattleMenuTagAttack:
                    CCLog("取消攻击");
                    ((RPGBattleSceneLayer*)this->m_parentNode)->enabledTouched(false);
                    ((RPGBattleSceneLayer*)this->m_parentNode)->cancelAllSelected();
                    
                    break;
                case kRPGBattleMenuTagSkill:
                    CCLog("取消技能列表");
                    
                    if(((RPGBattleSceneLayer*)this->m_parentNode)->getChildByTag(kRPGBattleSceneLayerTagSkillSelectDialog))
                        ((RPGBattleSceneLayer*)this->m_parentNode)->removeChildByTag(kRPGBattleSceneLayerTagSkillSelectDialog, true);
                    
                    ((RPGBattleSceneLayer*)this->m_parentNode)->enabledTouched(false);
                    ((RPGBattleSceneLayer*)this->m_parentNode)->cancelAllSelected();
                    
                    break;
                case kRPGBattleMenuTagItems:
                    CCLog("取消道具列表");
                    
                    if(((RPGBattleSceneLayer*)this->m_parentNode)->getChildByTag(kRPGBattleSceneLayerTagItemsSelectDialog))
                        ((RPGBattleSceneLayer*)this->m_parentNode)->removeChildByTag(kRPGBattleSceneLayerTagItemsSelectDialog, true);
                    
                    ((RPGBattleSceneLayer*)this->m_parentNode)->enabledTouched(false);
                    ((RPGBattleSceneLayer*)this->m_parentNode)->cancelAllSelected();
                    
                    break;
//                default:
//                    break;
            }
            
        }
            break;
        case kRPGBattleMenuTagCancel2:
        {
            //这个选择了一个技能或道具后,到了选择目标对象的一步,使用到的取消按钮
            
            CCMenuItemSprite *menuCancel = (CCMenuItemSprite*)this->getChildByTag(kRPGBattleMenuTagCancel);
            menuCancel->setVisible(true);
            
            CCMenuItemSprite *menuCancel2 = (CCMenuItemSprite*)this->getChildByTag(kRPGBattleMenuTagCancel2);
            menuCancel2->removeFromParentAndCleanup(true);
            
            if(this->m_parentNode->getChildByTag(kRPGBattleSceneLayerTagItemsSelectDialog))
                this->m_parentNode->getChildByTag(kRPGBattleSceneLayerTagItemsSelectDialog)->setVisible(true);
            
            if(this->m_parentNode->getChildByTag(kRPGBattleSceneLayerTagSkillSelectDialog))
                this->m_parentNode->getChildByTag(kRPGBattleSceneLayerTagSkillSelectDialog)->setVisible(true);
            
            ((RPGBattleSceneLayer*)this->m_parentNode)->enabledTouched(false);
            ((RPGBattleSceneLayer*)this->m_parentNode)->cancelAllSelected();
        }
            break;
        default:
        {
            CCLog("攻击");
            this->hideMenu();
            
            this->m_selectedMenuTag = kRPGBattleMenuTagAttack;
            
            CCMenuItemSprite *menuCancel = CCMenuItemSprite::create(CCSprite::createWithSpriteFrameName("commons_btn_back_04.png"), CCSprite::createWithSpriteFrameName("commons_btn_back_04.png"), this, menu_selector(RPGBattleMenu::onMenu));
            menuCancel->setPosition(ccp(43, 596));
            menuCancel->setTag(kRPGBattleMenuTagCancel);
            menuCancel->setScale(0.75);
            this->addChild(menuCancel);
            
            ((RPGBattleSceneLayer*)this->m_parentNode)->enabledTouched(true);
            
        }
            break;
    }
    
}