标签:cocos2d cocos2d-x 游戏 开发人员 ios8
UserDefault可以实现数据的存储,但是它的使用不能泛滥,具体讲一般情况下不会使用它保存大量的数据,它没有SQL语句那样的灵活。UserDefault除了保存游戏设置外,还有可以长期保持游戏精灵等对象的状态。#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使用UserDefault数据持久化实例:保存背景音乐和音效设置
标签:cocos2d cocos2d-x 游戏 开发人员 ios8
原文地址:http://blog.csdn.net/tonny_guan/article/details/40313673