Cocos2d-x学习笔记
菜单相关类
菜单相关类包含:菜单类Menu和菜单选项类MenuItem,Menu类派生于Layer。
文本菜单
文本菜单是菜单项,只能显示文本,文本菜单类包括MenuItemLabel、MenuItemFont和MenuItemAtlasFont。MenuItemLabel是个抽象类,具体使用的时候是MenuItemFont和MenuItemAtlasFont两个类。
文本菜单类MenuItemFont中的一个创建函数create定义如下:
static MenuItemFont * create ( const std::string & value ,//要显示的文本 const ccMenuCallback & callback)//菜单操作的回调函数指针文本菜单类MenuItemAtlasFont是基于图片集的文本菜单项,其中一个创建函数create定义如下:
static MenuItemAtlasFont * create ( cosnt std::string & value,//要显示的文本 const std::string & charMapFile,//图片集文件 int itemWidth,//要截取的文字在图片中的宽度 int itemHeight,//要截取的文字在图片中的高度 char startCharMap,//开始字符 consr ccMenuCallback & callback)//菜单操作的回调函数指针
实例
HelloWorld.h文件
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
// a selector callback
void menuItem1Callback(cocos2d::Ref * pSender);//菜单回调函数
void menuItem2Callback(cocos2d::Ref * pSender);//菜单回调函数
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
};
#endif // __HELLOWORLD_SCENE_H__
HelloWorld.cpp文件
#include "HelloWorldScene.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto 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 ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
/
// 2. add you code
Sprite * bg = Sprite::create("menu/background.png");
bg->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y +