UserDefault可以实现数据的存储,但是它的使用不能泛滥,具体讲一般情况下不会使用它保存大量的数据,它没有SQL语句那样的灵活。UserDefault除了保存游戏设置外,还有可以长期保持游戏精灵等对象的状态。
我们通过一个实例介绍一下在游戏项目中如何使用UserDefault。如图所示,在Setting场景中可以设置是否播放背景音乐和音效,现在我们将它完善将选择的状态保存到UserDefault中。
我们需要定义两个宏作为键,其中SOUND_KEY是音效状态键,MUSIC_KEY是背景音乐播放状态键。
#define SOUND_KEY "sound_key"
#define MUSIC_KEY "music_key"
但是这两个宏是需要在所有使用cpp文件中使用,我们可以创建一个头文件,把这些宏等都声明在这个头文件中,这个头文件SystemHeader.h代码如下:
#include "SimpleAudioEngine.h"
#define SOUND_KEY "sound_key"
#define MUSIC_KEY "music_key"
其中我们声明了两个宏,还有包含头文件SimpleAudioEngine.h,头文件SimpleAudioEngine.h是文件使用CocosDenshion引擎所需要的。
在Visual Studio 2012中添加SystemHeader.h文件过程是。首先,打开解决方案中的HelloWorld工程,右键点击Classes,在右键菜单中选择,“添加”→ “新建项”,如图14-8所示。
在弹出对话框中选择“Visual C++ ”→“头文件(.h) ”, 在下面名称中输入“SystemHeader.h”,然后点击“添加”按钮,添加头文件。
在使用的时候,我们需要将SystemHeader.h头文件添加到HelloWorldScene.h和SettingScene.h头文件中,示例代码如下:
- #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__
下面我们看看HelloWorld场景HelloWorldScene.cpp主要代码如下:
- #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");
- }
上述第①、②、③行代码中UserDefault::getInstance()->getBoolForKey(SOUND_KEY)是获得sound_key键值,通过取出布尔值判断是否播放音效。第④行代码UserDefault::getInstance()->getBoolForKey(MUSIC_KEY)是获得music_key键值,通过取出布尔值判断是否播放背景音乐。
下面我们再看看Setting场景SettingScene.cpp中的init()主要代码如下:
- #include "SettingScene.h"
- USING_NS_CC;
- using namespace CocosDenshion;
- bool Setting::init()
- {
- ... ...
- UserDefault *defaults = UserDefault::getInstance();
- if (defaults->getBoolForKey(MUSIC_KEY)) { ①
- musicToggleMenuItem->setSelectedIndex(0); ②