#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" #include "SettingScene.h" #include "SystemHeader.h" class HelloWorld : public cocos2d::Layer { public: … … CREATE_FUNC(HelloWorld); }; #endif // __HELLOWORLD_SCENE_H__
#include "HelloWorldScene.h" USING_NS_CC; using namespace CocosDenshion; bool HelloWorld::init() { ... ... return true; } void HelloWorld::menuItemSettingCallback(Ref* pSender) { auto sc = Setting::createScene(); auto reScene = TransitionJumpZoom::create(1.0f, sc); Director::getInstance()->pushScene(reScene); if (UserDefault::getInstance()->getBoolForKey(SOUND_KEY)) { ① SimpleAudioEngine::getInstance()->playEffect("sound/Blip.wav"); } } void HelloWorld::menuItemHelpCallback(Ref* pSender) { MenuItem* item = (MenuItem*)pSender; log("Touch Help %p", item); if (UserDefault::getInstance()->getBoolForKey(SOUND_KEY)) { ② SimpleAudioEngine::getInstance()->playEffect("sound/Blip.wav"); } } void HelloWorld::menuItemStartCallback(Ref* pSender) { MenuItem* item = (MenuItem*)pSender; log("Touch Start %p", item); if (UserDefault::getInstance()->getBoolForKey(SOUND_KEY)) { ③ SimpleAudioEngine::getInstance()->playEffect("sound/Blip.wav"); } } void HelloWorld::onEnter() { Layer::onEnter(); log("HelloWorld onEnter"); } void HelloWorld::onEnterTransitionDidFinish() { Layer::onEnterTransitionDidFinish(); log("HelloWorld onEnterTransitionDidFinish"); //播放 if (UserDefault::getInstance()->getBoolForKey(MUSIC_KEY)) { ④ SimpleAudioEngine::getInstance()->playBackgroundMusic("sound/Jazz.mp3", true); } } void HelloWorld::onExit() { Layer::onExit(); log("HelloWorld onExit"); } void HelloWorld::onExitTransitionDidStart() { Layer::onExitTransitionDidStart(); log("HelloWorld onExitTransitionDidStart"); } void HelloWorld::cleanup() { Layer::cleanup(); log("HelloWorld cleanup"); //停止 SimpleAudioEngine::getInstance()->stopBackgroundMusic("sound/Jazz.mp3"); }
#include "SettingScene.h" USING_NS_CC; using namespace CocosDenshion; bool Setting::init() { ... ... UserDefault *defaults = UserDefault::getInstance(); if (defaults->getBoolForKey(MUSIC_KEY)) { ① musicToggleMenuItem->setSelectedIndex(0); ② } else { musicToggleMenuItem->setSelectedIndex(1); ③ } if (defaults->getBoolForKey(SOUND_KEY)) { ④ soundToggleMenuItem->setSelectedIndex(0); ⑤ } else { soundToggleMenuItem->setSelectedIndex(1); ⑥ } return true; }
void Setting::menuSoundToggleCallback(Ref* pSender) { auto soundToggleMenuItem = (MenuItemToggle*)pSender; log("soundToggleMenuItem %d", soundToggleMenuItem->getSelectedIndex()); UserDefault *defaults = UserDefault::getInstance(); if (defaults->getBoolForKey(SOUND_KEY)) { ① defaults->setBoolForKey(SOUND_KEY, false); ② } else { defaults->setBoolForKey(SOUND_KEY, true); ③ SimpleAudioEngine::getInstance()->playEffect("sound/Blip.wav"); ④ } }
void Setting::menuMusicToggleCallback(Ref* pSender) { auto musicToggleMenuItem = (MenuItemToggle*)pSender; log("musicToggleMenuItem %d", musicToggleMenuItem->getSelectedIndex()); UserDefault *defaults = UserDefault::getInstance(); if (defaults->getBoolForKey(MUSIC_KEY)) { ① defaults->setBoolForKey(MUSIC_KEY, false); SimpleAudioEngine::getInstance()->stopBackgroundMusic(); } else { defaults->setBoolForKey(MUSIC_KEY, true); SimpleAudioEngine::getInstance()->playBackgroundMusic("sound/Synth.mp3"); } ② }
void Setting::onEnter() { Layer::onEnter(); log("Setting onEnter"); } void Setting::menuOkCallback(Ref* pSender) ① { Director::getInstance()->popScene(); if (UserDefault::getInstance()->getBoolForKey(SOUND_KEY)) { ② SimpleAudioEngine::getInstance()->playEffect("sound/Blip.wav"); } } void Setting::onEnterTransitionDidFinish() ③ { Layer::onEnterTransitionDidFinish(); log("Setting onEnterTransitionDidFinish"); if (UserDefault::getInstance()->getBoolForKey(MUSIC_KEY)) { ④ //播放 SimpleAudioEngine::getInstance()->playBackgroundMusic("sound/Synth.mp3", true); } } void Setting::onExit() { Layer::onExit(); log("Setting onExit"); } void Setting::onExitTransitionDidStart() { Layer::onExitTransitionDidStart(); log("Setting onExitTransitionDidStart"); } void Setting::cleanup() { Layer::cleanup(); log("Setting cleanup"); //停止 SimpleAudioEngine::getInstance()->stopBackgroundMusic("sound/Synth.mp3"); }
我们可以运行一下先将状态保存,然后重新运行游戏,看看是否能够保持状态。我们还可以到<Cocos2d-x工程目录>\proj.win32\Debug.win32目录下看看是否成功生成了UserDefault.xml文件。
《Cocos2d-x实战 C++卷》现已上线,各大商店均已开售:
京东:http://item.jd.com/11584534.html
亚马逊:http://www.amazon.cn/Cocos2d-x%E5%AE%9E%E6%88%98-C-%E5%8D%B7-%E5%85%B3%E4%B8%9C%E5%8D%87/dp/B00PTYWTLU
当当:http://product.dangdang.com/23606265.html
互动出版网:http://product.china-pub.com/3770734
《Cocos2d-x实战 C++卷》源码及样章下载地址:
源码下载地址:http://51work6.com/forum.php?mod=viewthread&tid=1155&extra=page%3D1
样章下载地址:http://51work6.com/forum.php?mod=viewthread&tid=1157&extra=page%3D1