转自:http://blog.csdn.net/gf771115/article/details/8940129
制作了一个游戏菜单的小例子,效果如下:
LayerMainMenu.h的代码如下:
[cpp] view plain copy print ?
- #pragma once
- #include "cocos2d.h"
- using namespace cocos2d;
- class LayerMainMenu
- : public CCLayer
- {
- public:
- LayerMainMenu(void);
- ~LayerMainMenu(void);
- virtual bool init();
- virtual void onEnterTransitionDidFinish();
- void newGame(CCObject *sender);
- void continueGame(CCObject *sender);
- void setting(CCObject *sender);
- void help(CCObject *sender);
- void exit(CCObject *sender);
- CREATE_FUNC(LayerMainMenu);
- };
#pragma once
#include "cocos2d.h"
using namespace cocos2d;
class LayerMainMenu
: public CCLayer
{
public:
LayerMainMenu(void);
~LayerMainMenu(void);
virtual bool init();
virtual void onEnterTransitionDidFinish();
void newGame(CCObject *sender);
void continueGame(CCObject *sender);
void setting(CCObject *sender);
void help(CCObject *sender);
void exit(CCObject *sender);
CREATE_FUNC(LayerMainMenu);
};
LayerMainMenu.cpp代码如下:
[cpp] view plain copy print ?
- #include "LayerMainMenu.h"
- LayerMainMenu::LayerMainMenu(void)
- {
- }
- LayerMainMenu::~LayerMainMenu(void)
- {
- }
- bool LayerMainMenu::init(){
- bool isRet = false;
- do
- {
- CC_BREAK_IF(! CCLayer::init());
-
- CCSprite* pSprite = CCSprite::create("Image/bg_menu.png");
- CC_BREAK_IF(! pSprite);
-
- CCSize size = CCDirector::sharedDirector()->getWinSize();
- pSprite->setPosition(ccp(size.width/2, size.height/2));
-
- this->addChild(pSprite);
-
- CCLayerColor *layerColor = CCLayerColor::create(ccc4(0,0,0,50));
- this->addChild(layerColor);
- CCSprite* spriteName = CCSprite::create("Image/name_game.png");
- spriteName->setPosition(ccp(size.width/2, size.height-150));
- this->addChild(spriteName);
-
- int gapLine = 40;
-
- CCMenuItemImage *menuNewGame = CCMenuItemImage::create("Image/button_new_game.png", "Image/button_new_game_pressed.png", this, menu_selector(LayerMainMenu::newGame));
- CCMenuItemImage *menuContinueGame = CCMenuItemImage::create("Image/button_continue_game.png", "Image/button_continue_game_pressed.png", this, menu_selector(LayerMainMenu::continueGame));
- CCMenuItemImage *menuSetting = CCMenuItemImage::create("Image/button_setting.png", "Image/button_setting_pressed.png", this, menu_selector(LayerMainMenu::setting));
- CCMenuItemImage *menuHelp = CCMenuItemImage::create("Image/button_help.png", "Image/button_help_pressed.png", this, menu_selector(LayerMainMenu::help));
- CCMenuItemImage *menuExit = CCMenuItemImage::create("Image/button_exit.png", "Image/button_exit_pressed.png", this, menu_selector(LayerMainMenu::exit));
-
- int heightItem = menuNewGame->getContentSize().height + gapLine;
-
- int yStart = size.height-150-spriteName->getContentSize().height - 50 + menuNewGame->getContentSize().height/2;
-
- menuNewGame->setPosition(ccp(0, 0));
- menuContinueGame->setPosition(ccp(0, -heightItem));
- menuSetting->setPosition(ccp(0, -heightItem*2));
- menuHelp->setPosition(ccp(0, - heightItem*3));
- menuExit->setPosition(ccp(0, - heightItem*4));
-
- CCMenu *menu = CCMenu::create(menuNewGame, menuContinueGame,menuSetting,menuHelp, menuExit, NULL);
- menu->setPosition(size.width/2, yStart);
- this->addChild(menu);
- isRet = true;
- } while (0);
- return isRet;
- }
- void LayerMainMenu::onEnterTransitionDidFinish(){
- }
- void LayerMainMenu::newGame(CCObject *sender){
- }
- void LayerMainMenu::continueGame(CCObject *sender){
- }
- void LayerMainMenu::setting(CCObject *sender){
- }
- void LayerMainMenu::help(CCObject *sender){
- }
- void LayerMainMenu::exit(CCObject *sender){
- }
#include "LayerMainMenu.h"
LayerMainMenu::LayerMainMenu(void)
{
}
LayerMainMenu::~LayerMainMenu(void)
{
}
bool LayerMainMenu::init(){
bool isRet = false;
do
{
CC_BREAK_IF(! CCLayer::init());
//背景图
CCSprite* pSprite = CCSprite::create("Image/bg_menu.png");
CC_BREAK_IF(! pSprite);
// Place the sprite on the center of the screen
CCSize size = CCDirector::sharedDirector()->getWinSize();
pSprite->setPosition(ccp(size.width/2, size.height/2));
// Add the sprite to HelloWorld layer as a child layer.
this->addChild(pSprite);
//添加一个半透明黑色图层
CCLayerColor *layerColor = CCLayerColor::create(ccc4(0,0,0,50));
this->addChild(layerColor);
CCSprite* spriteName = CCSprite::create("Image/name_game.png");
spriteName->setPosition(ccp(size.width/2, size.height-150));
this->addChild(spriteName);
//行间距
int gapLine = 40;
//菜单子项
CCMenuItemImage *menuNewGame = CCMenuItemImage::create("Image/button_new_game.png", "Image/button_new_game_pressed.png", this, menu_selector(LayerMainMenu::newGame));
CCMenuItemImage *menuContinueGame = CCMenuItemImage::create("Image/button_continue_game.png", "Image/button_continue_game_pressed.png", this, menu_selector(LayerMainMenu::continueGame));
CCMenuItemImage *menuSetting = CCMenuItemImage::create("Image/button_setting.png", "Image/button_setting_pressed.png", this, menu_selector(LayerMainMenu::setting));
CCMenuItemImage *menuHelp = CCMenuItemImage::create("Image/button_help.png", "Image/button_help_pressed.png", this, menu_selector(LayerMainMenu::help));
CCMenuItemImage *menuExit = CCMenuItemImage::create("Image/button_exit.png", "Image/button_exit_pressed.png", this, menu_selector(LayerMainMenu::exit));
//一个菜单子项和行间距高度的和
int heightItem = menuNewGame->getContentSize().height + gapLine;
//菜单的起始纵坐标
int yStart = size.height-150-spriteName->getContentSize().height - 50 + menuNewGame->getContentSize().height/2;
//设置所有菜单子项的坐标(相对于CCMenu的相对坐标)
menuNewGame->setPosition(ccp(0, 0));
menuContinueGame->setPosition(ccp(0, -heightItem));
menuSetting->setPosition(ccp(0, -heightItem*2));
menuHelp->setPosition(ccp(0, - heightItem*3));
menuExit->setPosition(ccp(0, - heightItem*4));
//创建菜单
CCMenu *menu = CCMenu::create(menuNewGame, menuContinueGame,menuSetting,menuHelp, menuExit, NULL);
menu->setPosition(size.width/2, yStart);
this->addChild(menu);
isRet = true;
} while (0);
return isRet;
}
void LayerMainMenu::onEnterTransitionDidFinish(){
}
void LayerMainMenu::newGame(CCObject *sender){
}
void LayerMainMenu::continueGame(CCObject *sender){
}
void LayerMainMenu::setting(CCObject *sender){
}
void LayerMainMenu::help(CCObject *sender){
}
void LayerMainMenu::exit(CCObject *sender){
}