精灵菜单类是MenuItemSprite,图片菜单类是MenuItemImage。由于MenuItemImage继承于MenuItemSprite,所以图片菜单也属于精灵菜单。为什么叫精灵菜单呢?那是因为这些菜单项具有精灵的特点,我们可以让精灵动起来,具体使用时候是把一个精灵放置到菜单中作为菜单项。
精灵菜单类MenuItemSprite,它的其中一个创建函数create定义如下:
?
[html]?view plaincopy
?
class="dp-xml" style="border: none; background-color: #ffffff; color: #5c5c5c; margin-bottom: 1px !important; margin-left: 45px !important;">
- <span?style="font-size:14px;">static?MenuItemSprite*?create??(?Node?*?normalSprite,??//菜单项正常显示时候的精灵??
- ?Node?*??selectedSprite,???????????????????????????????????????????????//选择菜单项时候的精灵??
- ?Node?*??disabledSprite,???????????????????????????????????????????????//菜单项禁用时候的精灵??
- ?const?ccMenuCallback?&?callback?????????????????????????????//菜单操作的回调函数指针??
- ?)</span>??
?
?
使用MenuItemSprite比较麻烦,在创建MenuItemSprite之前要先创建三种状态时候的精灵(即normalSprite、selectedSprite和disabledSprite)。MenuItemSprite还有一些create函数,在这些函数中可以省略disabledSprite参数。
如果精灵是由图片构成的,我们可以使用图片菜单MenuItemImage实现与精灵菜单同样的效果。MenuItemImage类的其中一个创建函数create定义如下:
?
[html]?view plaincopy
?
- <span?style="font-size:14px;">static?MenuItemImage*?create??(?const?std::string?&??normalImage,?//菜单项正常显示时候的图片??
- ?const?std::string?&?selectedImage,????????????????????????????????????????????????????????//选择菜单项时候的图片??
- ?const?std::string?&?disabledImage,??????????????????????????????????????????????//菜单项禁用时候的图片??
- ?const?ccMenuCallback?&?callback????????????????????????????????????????????????????????//菜单操作的回调函数指针??
- ?)</span>??
?
?
MenuItemImage还有一些create函数,在这些函数中可以省略disabledImage参数。
我们通过一个实例介绍一下精灵菜单和图片菜单的使用,这个实例如下图所示。
?
下面我们看看HelloWorldScene.cpp中init函数如下:
?
[html]?view plaincopy
?
- <span?style="font-size:14px;">bool?HelloWorld::init()??
- {??
- ???if?(?!Layer::init()?)??
- ???{??
- ???????return?false;??
- ???}??
- ?????
- ???Size?visibleSize?=?Director::getInstance()->getVisibleSize();??
- ???Point?origin?=?Director::getInstance()->getVisibleOrigin();??
- ???
- ???Sprite?*bg?=?Sprite::create("menu/background.png");?????
- ???bg->setPosition(Point(origin.x?+?visibleSize.width/2,??
- ?????????????????????????????origin.y?+?visibleSize.height/2));??
- ???this->addChild(bg);??
- ???
- ????//开始精灵??
- ???Sprite?*startSpriteNormal?=Sprite::create("menu/start-up.png");?????????????????????????????????????①??
- ???Sprite?*startSpriteSelected?=Sprite::create("menu/start-down.png");??????????????????????????????????????②??
- ???
- ????MenuItemSprite*startMenuItem?=?MenuItemSprite::create(startSpriteNormal,??
- ????????????????????????????????????????startSpriteSelected,??
- ?????????CC_CALLBACK_1(HelloWorld::menuItemStartCallback,this));?????????????????????????????????????③??
- ????startMenuItem->setPosition(Director::getInstance()->convertToGL(Point(700,170)));?????????????④??
- ?????
- ????//设置图片菜单??
- ??MenuItemImage?*settingMenuItem?=?MenuItemImage::create(??
- ????????????????????????????"menu/setting-up.png",??
- ????????????????????????????"menu/setting-down.png",??
- ?????????????????????????????CC_CALLBACK_1(HelloWorld::menuItemSettingCallback,this));?????????????⑤??
- ????settingMenuItem->setPosition(Director::getInstance()->convertToGL(Point(480,400)));??????????????????⑥??
- ?????
- ????//帮助图片菜单??
- ???MenuItemImage?*helpMenuItem?=?MenuItemImage::create(??
- ????????????????????????????"menu/help-up.png",??
- ????????????????????????????"menu/help-down.png",??
- ??????????????????????????????????CC_CALLBACK_1(HelloWorld::menuItemHelpCallback,this));??????????????????????⑦??
- ????helpMenuItem->setPosition(Director::getInstance()->convertToGL(Point(860,480)));????????????⑧??
- ???
- ????Menu*mu?=?Menu::create(startMenuItem,?settingMenuItem,?helpMenuItem,?NULL);??????????????????⑨??
- ????mu->setPosition(Point::ZERO);????????????????????????????????????????????????????????????????????????????????????????????????????⑩??
- ????this->addChild(mu);??
- ???
- ???return?true;??
- }</span>??
?
?
在上面的代码中第①~②行是创建两种不同状态的精灵,第③行代码是创建精灵菜单MenuItemSprite对象,第④行代码是设置开始菜单项(startMenuItem)位置,注意这个坐标是(700,170),由于(700, 170)的坐标是UI坐标,需要转换为OpenGL坐标。
第⑤和⑦行代码是创建图片菜单MenuItemImage对象,第⑥和⑧行代码是设置图片菜单位置。第⑨行代码是菜单Menu对象,第⑩行代码是菜单的位置mu->setPosition(Point::ZERO),设置的位置是Point::ZERO,它等价于Point(0.0f,0.0f)。
还有由于背景图片大小是1136 x 640,而Win32默认窗口大小是960 x 640,我们需要重新设置大小,修改AppDelegate.cpp代码如下:
?
?
?
[html]?view plaincopy
?
- bool?HelloWorld::init()??
- {??
- ???if?(?!Layer::init()?)??
- ???{??
- ???????return?false;??
- ???}??
- ?????
- ???Size?visibleSize?=?Director::getInstance()->getVisibleSize();??
- ???Point?origin?=?Director::getInstance()->getVisibleOrigin();??
- ???
- ???Sprite?*bg?=?Sprite::create("menu/background.png");?????
- ???bg->setPosition(Point(origin.x?+?visibleSize.width/2,??
- ?????????????????????????????origin.y?+?visibleSize.height/2));??
- ???this->addChild(bg);??
- ???
- ????//开始精灵??
- ???Sprite?*startSpriteNormal?=Sprite::create("menu/start-up.png");?????????????????????????????????????①??
- ???Sprite?*startSpriteSelected?=Sprite::create("menu/start-down.png");??????????????????????????????????????②??
- ???
- ????MenuItemSprite*startMenuItem?=?MenuItemSprite::create(startSpriteNormal,??
- ????????????????????????????????????????startSpriteSelected,??
- ?????????CC_CALLBACK_1(HelloWorld::menuItemStartCallback,this));?????????????????????????????????????③??
- ????startMenuItem->setPosition(Director::getInstance()->convertToGL(Point(700,170)));?????????????④??
- ?????
- ????//设置图片菜单??
- ??MenuItemImage?*settingMenuItem?=?MenuItemImage::create(??
- ????????????????????????????"menu/setting-up.png",??
- ????????????????????????????"menu/setting-down.png",??
- ?????????????????????????????CC_CALLBACK_1(HelloWorld::menuItemSettingCallback,this));?????????????⑤??
- ????settingMenuItem->setPosition(Director::getInstance()->convertToGL(Point(480,400)));??????????????????⑥??
- ?????
- ????//帮助图片菜单??
- ???MenuItemImage?*helpMenuItem?=?MenuItemImage::create(??
- ????????????????????????????"menu/help-up.png",??
- ????????????????????????????"menu/help-down.png",??
- ??????????????????????????????????CC_CALLBACK_1(HelloWorld::menuItemHelpCallback,this));??????????????????????⑦??
- ????helpMenuItem->setPosition(Director::getInstance()->convertToGL(Point(860,480)));????????????⑧??
- ???
- ????Menu*mu?=?Menu::create(startMenuItem,?settingMenuItem,?helpMenuItem,?NULL);??????????????????⑨??
- ????mu->setPosition(Point::ZERO);????????????????????????????????????????????????????????????????????????????????????????????????????⑩??
- ????this->addChild(mu);??
- ???
- ???return?true;??
- }??
在上面的代码中第①~②行是创建两种不同状态的精灵,第③行代码是创建精灵菜单MenuItemSprite对象,第④行代码是设置开始菜单项(startMenuItem)位置,注意这个坐标是(700,170),由于(700, 170)的坐标是UI坐标,需要转换为OpenGL坐标。
第⑤和⑦行代码是创建图片菜单MenuItemImage对象,第⑥和⑧行代码是设置图片菜单位置。第⑨行代码是菜单Menu对象,第⑩行代码是菜单的位置mu->setPosition(Point::ZERO),设置的位置是Point::ZERO,它等价于Point(0.0f,0.0f)。
还有由于背景图片大小是1136 x 640,而Win32默认窗口大小是960 x 640,我们需要重新设置大小,修改AppDelegate.cpp代码如下:
?
?
[html]?view plaincopy
?
- <pre?code_snippet_id="340364"?snippet_file_name="blog_20140512_5_4431376"?name="code"?class="html">boolAppDelegate::applicationDidFinishLaunching()?{??
- ???//?initialize?director??
- ???auto?director?=?Director::getInstance();??
- ???auto?glview?=?director->getOpenGLView();??
- ???if(!glview)?{??
- ???????glview?=?GLView::create("My?Game");??
- ?????????????glview->setFrameSize(1136,?640);??????????????????????????????????????????????????????????????????????????????①??
- ???????director->setOpenGLView(glview);??
- ???}??
- ???
- …?…??
- }</pre>??
[html]?view plaincopy
?
- ??
我们需要在第①行添加glview->setFrameSize(1136, 640)代码。