设置场景(Setting),Setting.h文件代码如下:
- #ifndef __Setting_SCENE_H__
- #define __Setting_SCENE_H__
- #include "cocos2d.h"
- #include "SimpleAudioEngine.h"
- class Setting : public cocos2d::Layer
- {
- bool isEffect; ①
- public:
- static cocos2d::Scene* createScene();
- virtual bool init();
- virtual void onEnter();
- virtual void onEnterTransitionDidFinish();
- virtual void onExit();
- virtual void onExitTransitionDidStart();
- virtual void cleanup();
- void menuSoundToggleCallback(cocos2d::Ref* pSender);
- void menuMusicToggleCallback(cocos2d::Ref* pSender);
- void menuOkCallback(cocos2d::Ref* pSender);
- CREATE_FUNC(Setting);
- };
- #endif // __Setting_SCENE_H__
- #define __HELLOWORLD_SCENE_H__
上述代码第①是定义布尔类型成员变量isEffect,它用来保存现在是否允许音效,事实上是否允许音效和背景音乐播放状态应该持久化到文件中,由于我们还没有介绍数据持久化,所以我们采用isEffect变量记录状态,当我们在后面的章节学习完成数据持久化后,再保存它们状态。其它的代码与HelloWorld场景代码非常相似,这里不再赘述。Setting.cpp文件代码如下:
- #include "SettingScene.h"
- USING_NS_CC;
- using namespace CocosDenshion;
- … …
- bool Setting::init()
- {
- if ( !Layer::init() )
- {
- return false;
- }
- log("Setting init");
- Size visibleSize = Director::getInstance()->getVisibleSize();
- Point origin = Director::getInstance()->getVisibleOrigin();
- Sprite *bg = Sprite::create("setting-back.png");
- bg->setPosition(Point(origin.x + visibleSize.width/2,
- origin.y + visibleSize.height /2));
- this->addChild(bg);
- //音效
- auto soundOnMenuItem = MenuItemImage::create(
- "on.png",
- "on.png");
- auto soundOffMenuItem = MenuItemImage::create(
- "off.png",
- "off.png");
- auto soundToggleMenuItem = MenuItemToggle::createWithCallback(
- CC_CALLBACK_1(Setting::menuSoundToggleCallback, this),
- soundOffMenuItem,
- soundOnMenuItem,
- NULL);
- soundToggleMenuItem->setPosition(Director::getInstance()->convertToGL(Point(818, 220)));
- //背景音乐
- auto musicOnMenuItem = MenuItemImage::create(
- "on.png",
- "on.png");
- auto musicOffMenuItem = MenuItemImage::create(
- "off.png",
- "off.png");
- auto musicToggleMenuItem = MenuItemToggle::createWithCallback(
- CC_CALLBACK_1(Setting::menuMusicToggleCallback, this),
- musicOffMenuItem,
- musicOnMenuItem,
- NULL );
- musicToggleMenuItem->setPosition(Director::getInstance()->convertToGL(Point(818, 362)));
- //Ok按钮
- auto okMenuItem = MenuItemImage::create(
- "ok-down.png",
- "ok-up.png",
- CC_CALLBACK_1(Setting::menuOkCallback, this));
- okMenuItem->setPosition(Director::getInstance()->convertToGL(Point(600, 510)));
- Menu* mn = Menu::create(soundToggleMenuItem, musicToggleMenuItem,okMenuItem, NULL);
- mn->setPosition(Point::ZERO);
- this->addChild(mn);
- return true;
- }
- void Setting::menuOkCallback(Ref* pSender)
- {
- Director::getInstance()->popScene();
- if (isEffect) {
- SimpleAudioEngine::getInstance()->playEffect("sound/Blip.wav"); ①
- }
- }
- void Setting::menuSoundToggleCallback(Ref* pSender) ②
- {
- auto soundToggleMenuItem = (MenuItemToggle*)pSender;
- log("soundToggleMenuItem %d", soundToggleMenuItem->getSelectedIndex());
- border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接