新建工程,名为textMenu
修改HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::CCLayer
{
public:
// Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
virtual bool init();
// there's no 'id' in cpp, so we recommend to return the class instance pointer
static cocos2d::CCScene* scene();
// a selector callback
void menuFun();
// preprocessor macro for "static create()" constructor ( node() deprecated )
CREATE_FUNC(HelloWorld);
};
#endif // __HELLOWORLD_SCENE_H__
修改HelloWorldScene.cpp#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
using namespace cocos2d;
using namespace CocosDenshion;
CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
//--------CCMenuItemFont
CCMenuItemFont *itemFont=CCMenuItemFont::create("开始游戏",this,menu_selector(HelloWorld::menuFun));
itemFont->setPosition(ccp(-120, 80));
//--------CCMenuItemImage
CCMenuItemImage *imageItem=CCMenuItemImage::create("CloseNormal.png", "CloseSelected.png",this,menu_selector(HelloWorld::menuFun));
imageItem->setPosition(ccp(-20, 80));
//---------CCMenuItemLabel
CCLabelTTF *lableTTF = CCLabelTTF::create("返回主菜单", "Helvetica", 20);
CCMenuItemLabel *labelItem=CCMenuItemLabel::create(lableTTF, this, menu_selector(HelloWorld::menuFun));
labelItem->setPosition(ccp(80, 80));
//---------CCMenuItemSprite
CCSprite *spNor=CCSprite::create("Icon.png");
spNor->setColor(ccc3(255, 255, 0));
CCSprite *spSelected=CCSprite::create("Icon.png");
CCMenuItemSprite* spriteItem=CCMenuItemSprite::create(spNor, spSelected,this,menu_selector(HelloWorld::menuFun));
spriteItem->setPosition(ccp(-80, -20));
//---------CCMenuItemToggle
CCMenuItemFont *fontItem1=CCMenuItemFont::create("音乐开");
CCMenuItemFont *fontItem2=CCMenuItemFont::create("音乐关");
CCMenuItemToggle *toggleItem=CCMenuItemToggle::createWithTarget(this, menu_selector(HelloWorld::menuFun),fontItem1,fontItem2,NULL);
toggleItem->setPosition(ccp(20, -20));
CCMenu *menu=CCMenu::create(itemFont,imageItem,labelItem,spriteItem,toggleItem,NULL);
addChild(menu);
return true;
}
void HelloWorld::menuFun()
{
}
创建菜单是先创建菜单项,再创建菜单,把菜单项加入其中,并以空结尾。