cocos2d-x 菜单CCMenu和CCMenuItemImage应用实例

转自:http://blog.csdn.net/gf771115/article/details/8940129


制作了一个游戏菜单的小例子,效果如下:

cocos2d-x 菜单CCMenu和CCMenuItemImage应用实例_第1张图片

LayerMainMenu.h的代码如下:

[cpp] view plain copy print ?
  1. #pragma once
  2. #include "cocos2d.h"
  3. using namespace cocos2d;
  4. class LayerMainMenu
  5. : public CCLayer
  6. {
  7. public:
  8. LayerMainMenu(void);
  9. ~LayerMainMenu(void);
  10. virtual bool init();
  11. virtual void onEnterTransitionDidFinish();
  12. void newGame(CCObject *sender);
  13. void continueGame(CCObject *sender);
  14. void setting(CCObject *sender);
  15. void help(CCObject *sender);
  16. void exit(CCObject *sender);
  17. CREATE_FUNC(LayerMainMenu);
  18. };

LayerMainMenu.cpp代码如下:

[cpp] view plain copy print ?
  1. #include "LayerMainMenu.h"
  2. LayerMainMenu::LayerMainMenu(void)
  3. {
  4. }
  5. LayerMainMenu::~LayerMainMenu(void)
  6. {
  7. }
  8. bool LayerMainMenu::init(){
  9. bool isRet = false;
  10. do
  11. {
  12. CC_BREAK_IF(! CCLayer::init());
  13. //背景图
  14. CCSprite* pSprite = CCSprite::create("Image/bg_menu.png");
  15. CC_BREAK_IF(! pSprite);
  16. // Place the sprite on the center of the screen
  17. CCSize size = CCDirector::sharedDirector()->getWinSize();
  18. pSprite->setPosition(ccp(size.width/2, size.height/2));
  19. // Add the sprite to HelloWorld layer as a child layer.
  20. this->addChild(pSprite);
  21. //添加一个半透明黑色图层
  22. CCLayerColor *layerColor = CCLayerColor::create(ccc4(0,0,0,50));
  23. this->addChild(layerColor);
  24. CCSprite* spriteName = CCSprite::create("Image/name_game.png");
  25. spriteName->setPosition(ccp(size.width/2, size.height-150));
  26. this->addChild(spriteName);
  27. //行间距
  28. int gapLine = 40;
  29. //菜单子项
  30. CCMenuItemImage *menuNewGame = CCMenuItemImage::create("Image/button_new_game.png", "Image/button_new_game_pressed.png", this, menu_selector(LayerMainMenu::newGame));
  31. CCMenuItemImage *menuContinueGame = CCMenuItemImage::create("Image/button_continue_game.png", "Image/button_continue_game_pressed.png", this, menu_selector(LayerMainMenu::continueGame));
  32. CCMenuItemImage *menuSetting = CCMenuItemImage::create("Image/button_setting.png", "Image/button_setting_pressed.png", this, menu_selector(LayerMainMenu::setting));
  33. CCMenuItemImage *menuHelp = CCMenuItemImage::create("Image/button_help.png", "Image/button_help_pressed.png", this, menu_selector(LayerMainMenu::help));
  34. CCMenuItemImage *menuExit = CCMenuItemImage::create("Image/button_exit.png", "Image/button_exit_pressed.png", this, menu_selector(LayerMainMenu::exit));
  35. //一个菜单子项和行间距高度的和
  36. int heightItem = menuNewGame->getContentSize().height + gapLine;
  37. //菜单的起始纵坐标
  38. int yStart = size.height-150-spriteName->getContentSize().height - 50 + menuNewGame->getContentSize().height/2;
  39. //设置所有菜单子项的坐标(相对于CCMenu的相对坐标)
  40. menuNewGame->setPosition(ccp(0, 0));
  41. menuContinueGame->setPosition(ccp(0, -heightItem));
  42. menuSetting->setPosition(ccp(0, -heightItem*2));
  43. menuHelp->setPosition(ccp(0, - heightItem*3));
  44. menuExit->setPosition(ccp(0, - heightItem*4));
  45. //创建菜单
  46. CCMenu *menu = CCMenu::create(menuNewGame, menuContinueGame,menuSetting,menuHelp, menuExit, NULL);
  47. menu->setPosition(size.width/2, yStart);
  48. this->addChild(menu);
  49. isRet = true;
  50. } while (0);
  51. return isRet;
  52. }
  53. void LayerMainMenu::onEnterTransitionDidFinish(){
  54. }
  55. void LayerMainMenu::newGame(CCObject *sender){
  56. }
  57. void LayerMainMenu::continueGame(CCObject *sender){
  58. }
  59. void LayerMainMenu::setting(CCObject *sender){
  60. }
  61. void LayerMainMenu::help(CCObject *sender){
  62. }
  63. void LayerMainMenu::exit(CCObject *sender){
  64. }



你可能感兴趣的:(cocos2d-x 菜单CCMenu和CCMenuItemImage应用实例)