#ifndef __SceneHello__CCRadioMenu__ #define __SceneHello__CCRadioMenu__ #include "cocos2d.h" class CCRadioMenu : public cocos2d::CCMenu{ public: CCRadioMenu():cocos2d::CCMenu(){} cocos2d::CCMenuItem *_curHighlighted; virtual bool ccTouchBegan(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent); virtual void ccTouchEnded(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent); virtual void ccTouchCancelled(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent); virtual void ccTouchMoved(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent); void setSelectedItem_(cocos2d::CCMenuItem *item); static CCRadioMenu* create(cocos2d::CCMenuItem* item,...); }; #endif /* defined(__SceneHello__CCRadioMenu__) */
#include "CCRadioMenu.h" using namespace cocos2d; CCRadioMenu *CCRadioMenu::create(CCMenuItem *item,...){ va_list args; va_start(args,item); CCRadioMenu *pRet = new CCRadioMenu(); CCArray* pArray = NULL; if( item ) { pArray = CCArray::create(item,NULL); CCMenuItem *i = va_arg(args,CCMenuItem*); while(i) { pArray->addObject(i); i = va_arg(args,CCMenuItem*); } } if (pRet && pRet->initWithArray(pArray)) { pRet->autorelease(); va_end(args); return pRet; } va_end(args); CC_SAFE_DELETE(pRet); return NULL; } void CCRadioMenu::setSelectedItem_(CCMenuItem *item){ m_pSelectedItem = NULL; m_pSelectedItem = item; } bool CCRadioMenu::ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent){ if (m_eState != kCCMenuStateWaiting) return false; CCMenuItem *curSelection = this->itemForTouch(pTouch); if (curSelection) { curSelection->selected(); _curHighlighted = curSelection; if (_curHighlighted) { if (m_pSelectedItem != curSelection) { m_pSelectedItem->unselected(); } m_eState = kCCMenuStateTrackingTouch; return true; } } return false; } void CCRadioMenu::ccTouchEnded(CCTouch *pTouch,CCEvent *pEvent){ CCAssert(m_eState == kCCMenuStateTrackingTouch,"[Menu ccTouchEnded] -- invalid state"); CCMenuItem *curSelection = this->itemForTouch(pTouch); if (curSelection != _curHighlighted && curSelection != NULL) { m_pSelectedItem->selected(); _curHighlighted->unselected(); _curHighlighted = NULL; m_eState = kCCMenuStateWaiting; return; } m_pSelectedItem = _curHighlighted; _curHighlighted->activate(); _curHighlighted = NULL; m_eState = kCCMenuStateWaiting; } void CCRadioMenu::ccTouchCancelled(CCTouch *pTouch,CCEvent *pEvent){ CCAssert(m_eState == kCCMenuStateTrackingTouch,"[Menu ccTouchCancelled] -- invalid state"); m_pSelectedItem->selected(); _curHighlighted->unselected(); _curHighlighted = NULL; m_eState = kCCMenuStateWaiting; } void CCRadioMenu::ccTouchMoved(CCTouch *pTouch,"[Menu ccTouchMoved] -- invalid state"); CCMenuItem *curSelection = this->itemForTouch(pTouch); if (curSelection != _curHighlighted && curSelection != NULL) { _curHighlighted->unselected(); curSelection->selected(); _curHighlighted = curSelection; return; } }用法:
CCMenuItemImage *menuItem_man = CCMenuItemImage::create( "jsman_nor.png","jsman_pre.png",this,menu_selector(HelloWorld::selectMan)); menuItem_man->setPosition(ccp(size.width*0.3f,size.height*0.66f)); CCMenuItemImage *menuItem_man2 = CCMenuItemImage::create( "jsman_nor.png",menu_selector(HelloWorld::selectMan)); menuItem_man2->setPosition(ccp(size.width*0.3f,size.height*0.3f)); CCMenuItemImage *menuItem_woman = CCMenuItemImage::create( "jswoman_nor.png","jswoman_pre.png",menu_selector(HelloWorld::selectWomen)); menuItem_woman->setPosition(ccp(size.width*0.7f,size.height*0.66f)); CCMenuItemImage *menuItem_woman2 = CCMenuItemImage::create( "jswoman_nor.png",menu_selector(HelloWorld::selectWomen)); menuItem_woman2->setPosition(ccp(size.width*0.7f,size.height*0.3f)); CCRadioMenu *radioMenu = CCRadioMenu::create(menuItem_man,menuItem_man2,menuItem_woman,menuItem_woman2,NULL); radioMenu->setSelectedItem_(menuItem_man); menuItem_man->selected(); radioMenu->setPosition(CCPointZero); this->addChild(radioMenu,1);原文链接:https://www.f2er.com/cocos2dx/344200.html