//CAlert.h
1 #ifndef __CCALERT_H__ 2 #define __CCALERT_H__ 3 #include "cocos2d.h" 4 USING_NS_CC; 5 enum AlertType{ 6 AlertType_OkAndCancel=0, 7 AlertType_OkOnly, 8 }; 9 enum{ 10 Item_OK_tag=0, 11 Item_Cancel_tag, 12 }; 13 class CCAlert; 14 class CCAlertDelegate 15 { 16 public: 17 virtual void onOk(CCAlert* pSender){}; 18 virtual void onCancel(CCAlert* pSender){}; 19 }; 20 21 22 class CCAlert: public CCLayer, public CCAlertDelegate 23 { 24 public: 25 CCAlert(void); 26 ~CCAlert(void); 27 static CCAlert* create(int alertTag, CCAlertDelegate* del, AlertType type=AlertType_OkAndCancel); 28 bool init(int alertTag, CCAlertDelegate* del, AlertType type); 29 30 void alertFunc(CCObject* pSender); 31 32 virtual void onEnter(); 33 virtual void onExit(); 34 35 virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); 36 virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent); 37 protected: 38 AlertType m_alertType; 39 CCAlertDelegate* m_delegate; 40 }; 41 42 #endif
//CAlert.cpp
1 #include "CAlert.h" 2 3 4 CCAlert::CCAlert(void):m_delegate(NULL) 5 { 6 } 7 8 9 CCAlert::~CCAlert(void) 10 { 11 } 12 13 CCAlert* CCAlert::create(int alertTag, CCAlertDelegate* del, AlertType type){ 14 CCAlert* alert = new CCAlert; 15 if (alert&& alert->init(alertTag, del, type)) 16 { 17 alert->autorelease(); 18 }else{ 19 CC_SAFE_DELETE(alert); 20 alert = NULL; 21 } 22 23 return alert; 24 } 25 26 bool CCAlert::init(int alertTag, CCAlertDelegate* del, AlertType type){ 27 CCLayer::init(); 28 m_delegate = del; 29 m_alertType = type; 30 31 32 CCSize winSize = CCDirector::sharedDirector()->getWinSize(); 33 CCLayerColor* colorLayer = CCLayerColor::create(ccc4(100, 100, 100, 100)); 34 colorLayer->setContentSize(winSize); 35 colorLayer->setAnchorPoint(CCPointZero); 36 addChild(colorLayer); 37 38 //onOk item 39 CCMenuItemFont* okItem = CCMenuItemFont::create("OK", this, menu_selector(CCAlert::alertFunc)); 40 okItem->setTag(Item_OK_tag); 41 //onCancel item 42 CCMenuItemFont* cancelItem = CCMenuItemFont::create("Cancel", this, menu_selector(CCAlert::alertFunc)); 43 cancelItem->setTag(Item_Cancel_tag); 44 CCMenu* menu = CCMenu::create(); 45 switch (m_alertType) 46 { 47 case AlertType_OkAndCancel: 48 { 49 menu->addChild(okItem); 50 menu->addChild(cancelItem); 51 menu->alignItemsHorizontallyWithPadding(winSize.width*0.2); 52 }break; 53 case AlertType_OkOnly: 54 { 55 menu->addChild(okItem); 56 }break; 57 default:break; 58 } 59 60 menu->setPosition(ccp(winSize.width/2, winSize.height*0.2)); 61 addChild(menu); 62 return true; 63 } 64 65 void CCAlert::alertFunc(CCObject* pSender){ 66 int tag = ((CCMenuItem*)pSender)->getTag(); 67 switch (tag) 68 { 69 case Item_OK_tag: 70 { 71 m_delegate->onOk(this); 72 }break; 73 case Item_Cancel_tag: 74 { 75 m_delegate->onCancel(this); 76 }break; 77 default:break; 78 } 79 80 this->removeFromParentAndCleanup(true); 81 } 82 83 void CCAlert::onEnter(){ 84 CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, -128, true); 85 CCLayer::onEnter(); 86 } 87 88 void CCAlert::onExit(){ 89 CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this); 90 CCLayer::onExit(); 91 } 92 93 bool CCAlert::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent){ 94 return true; 95 } 96 97 void CCAlert::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent){ 98 99 }
调用示例:
//BKGameManager.h
1 #ifndef __BKGAMEMENULAYER_H__ 2 #define __BKGAMEMENULAYER_H__ 3 #include "cocos2d.h" 4 USING_NS_CC; 5 #include "CAlert.h" 6 class BKGameMenuLayer: public CCLayer, public CCAlertDelegate 7 { 8 public: 9 BKGameMenuLayer(void); 10 ~BKGameMenuLayer(void); 11 12 static BKGameMenuLayer* create(); 13 bool init(); 14 15 void onOk(CCAlert* pSender); 16 void onCancel(CCAlert* pSender); 17 private: 18 void gotoCheckPointLayer(CCObject* pSender); 19 void gotoGameConfigFunc(CCObject* pSender); 20 void gotoGameAbout(CCObject* pSender); 21 22 private: 23 CCSprite* mMenuBk; 24 25 }; 26 27 #endif
//BKGameManager.cpp
1 #include "BKGameMenuLayer.h" 2 #include "BKGameManager.h" 3 4 5 BKGameMenuLayer::BKGameMenuLayer(void) 6 { 7 } 8 9 10 BKGameMenuLayer::~BKGameMenuLayer(void) 11 { 12 CC_SAFE_RELEASE(mMenuBk); 13 } 14 15 BKGameMenuLayer* BKGameMenuLayer::create(){ 16 BKGameMenuLayer* menuLayer = new BKGameMenuLayer(); 17 if (menuLayer && menuLayer->init()) 18 { 19 menuLayer->autorelease(); 20 }else{ 21 CC_SAFE_DELETE(menuLayer); 22 menuLayer = NULL; 23 } 24 25 return menuLayer; 26 } 27 28 bool BKGameMenuLayer::init(){ 29 CCLayer::init(); 30 CCSize winSize = CCDirector::sharedDirector()->getWinSize(); 31 mMenuBk = CCSprite::create("bk/bk_1.png"); 32 mMenuBk->retain(); 33 mMenuBk->setAnchorPoint(CCPointZero); 34 addChild(mMenuBk); 35 36 //game title 37 CCLabelTTF* gameTitle = CCLabelTTF::create("B K D R", "Marker Felt", 40); 38 gameTitle->setPosition(ccp(winSize.width/2, winSize.height*0.8f)); 39 mMenuBk->addChild(gameTitle); 40 41 //menu 42 CCMenuItemFont* startItem = CCMenuItemFont::create("StartGame", this, menu_selector(BKGameMenuLayer::gotoCheckPointLayer)); 43 CCMenuItemFont* setupItem = CCMenuItemFont::create("Config", this, menu_selector(BKGameMenuLayer::gotoGameConfigFunc)); 44 CCMenuItemFont* aboutItem = CCMenuItemFont::create("About", this, menu_selector(BKGameMenuLayer::gotoGameAbout)); 45 46 CCMenu* gameMnu = CCMenu::create(startItem, setupItem, aboutItem, NULL); 47 gameMnu->setPosition(ccp(winSize.width/2, winSize.height*0.4f)); 48 gameMnu->alignItemsVerticallyWithPadding(40); 49 mMenuBk->addChild(gameMnu); 50 return true; 51 } 52 53 54 // 55 void BKGameMenuLayer::gotoCheckPointLayer(CCObject* pSender){ 56 BKGameManager::shareGameManager()->gotoCheckPointLayer(); 57 } 58 59 void BKGameMenuLayer::gotoGameConfigFunc(CCObject* pSender){ 60 BKGameManager::shareGameManager()->gotoConfigLayer(); 61 } 62 63 void BKGameMenuLayer::gotoGameAbout(CCObject* pSender){ 64 CCAlert* alert = CCAlert::create(1, this, AlertType_OkAndCancel); 65 addChild(alert); 66 } 67 68 69 //delegate 70 void BKGameMenuLayer::onOk(CCAlert* pSender){ 71 CCLOG("ok"); 72 } 73 74 void BKGameMenuLayer::onCancel(CCAlert* pSender){ 75 CCLOG("cancel"); 76 }
附上调试结果截图:
有点懒,只把源码贴出来了,大家不要介意,如果大家有更好的实现方法或者用上面的源码出问题了,大家给我留言啊,呵呵