Cocos2d-x 深入解析系列 : 以XML文件方式保存用户数据

来源:互联网 发布:电脑认证淘宝开店时间 编辑:程序博客网 时间:2024/06/05 19:25

        Cocos2d-x 深入解析系列 : 以XML文件方式保存用户数据       

        分类:            Cocos2d-x学习3884人阅读评论(4)收藏举报
Cocos2d-x

目录(?)[+]

  1. Cocos2d-x相关教程来源于红孩儿的游戏编程之路CSDN博客地址httpblogcsdnnethonghaier红孩儿Cocos2d-X学习园地QQ3群20510014947870848
  2. Cocos2d-x 深入解析系列以XML文件方式保存用户数据

[Cocos2d-x相关教程来源于红孩儿的游戏编程之路CSDN博客地址http://blog.csdn.net/honghaier

红孩儿Cocos2d-X学习园地QQ3群:205100149,47870848

          Cocos2d-x 深入解析系列:以XML文件方式保存用户数据

另:本章所用Cocos2d-x版本为:

2.1.1 (2013-01-28)


              大家好,今天我们来学习一下如何使用XML文件方式来保存游戏中的用户数据。在使用Cocos2d-x开发游戏的过程中,我们经常会使用XML来存储用户存档数据,而这些XML我们该如何生成呢?Cocos2d-x提供了一个类CCUserDefault以方便我们随时将需要的数据生成XML文件。

打开CCUserDefault.h:

[cpp] view plaincopyprint?
  1. #ifndef __SUPPORT_CCUSERDEFAULT_H__ 
  2. #define __SUPPORT_CCUSERDEFAULT_H__ 
  3. //加入平台所用的头文件 
  4. #include "platform/CCPlatformMacros.h" 
  5. #include <string> 
  6. //使用Cocos2d命名空间 
  7. NS_CC_BEGIN 
  8.  
  9. //定义类CCUserDefault 
  10. class CC_DLL CCUserDefault 
  11. public
  12.     //析构 
  13.     ~CCUserDefault(); 
  14.  
  15.     //从指定的键中取得布尔值 
  16.     bool    getBoolForKey(constchar* pKey); 
  17.     //从指定的键中取得布尔值,如果没有则返回默认参数 
  18.     bool    getBoolForKey(constchar* pKey, bool defaultValue); 
  19.     //从指定的键中取得整数值 
  20.     int     getIntegerForKey(constchar* pKey); 
  21.     //从指定的键中取得整数值,如果没有则返回默认参数 
  22.     int     getIntegerForKey(constchar* pKey, int defaultValue); 
  23.      //从指定的键中取得浮点值 
  24.     float    getFloatForKey(constchar* pKey); 
  25.     //从指定的键中取得浮点值,如果没有则返回默认参数 
  26.     float    getFloatForKey(constchar* pKey, float defaultValue); 
  27.      //从指定的键中取得双精度值 
  28.     double  getDoubleForKey(constchar* pKey); 
  29.     //从指定的键中取得双精度值,如果没有则返回默认参数 
  30.     double  getDoubleForKey(constchar* pKey, double defaultValue); 
  31.      //从指定的键中取得字符串值 
  32.     std::string getStringForKey(constchar* pKey); 
  33.     //从指定的键中取得字符串值,如果没有则返回默认参数 
  34.     std::string getStringForKey(constchar* pKey, const std::string & defaultValue); 
  35.  
  36.     //设置指定键的布尔值 
  37.     void    setBoolForKey(constchar* pKey, bool value); 
  38.     //设置指定键的整数值 
  39.     void    setIntegerForKey(constchar* pKey, int value); 
  40.     //设置指定键的浮点值 
  41.     void    setFloatForKey(constchar* pKey, float value); 
  42.     //设置指定键的双精度值 
  43.     void    setDoubleForKey(constchar* pKey, double value); 
  44.     //设置指定键的字符串值 
  45.     void    setStringForKey(constchar* pKey, const std::string & value); 
  46.     //立即将XML数据写入文件 
  47.     void    flush(); 
  48.     //取得单例的指针 
  49.     static CCUserDefault* sharedUserDefault(); 
  50.     //释放单例 
  51.     static void purgeSharedUserDefault(); 
  52.     //取得保存后的XML文件路径 
  53.     const static std::string& getXMLFilePath(); 
  54.  
  55. private
  56.     //因为是单例,构造函数私有化 
  57.     CCUserDefault(); 
  58.     //创建XML文件 
  59.     static bool createXMLFile(); 
  60.     //XML文件是否存在 
  61.     static bool isXMLFileExist(); 
  62.     //初始化XML文件 
  63.     static void initXMLFilePath(); 
  64.     //单例的指针 
  65.     static CCUserDefault* m_spUserDefault; 
  66.     //XML文件的路径 
  67.     static std::string m_sFilePath; 
  68.     //XML文件是否已经被初始化 
  69.     static bool m_sbIsFilePathInitialized; 
  70. }; 
  71.  
  72. NS_CC_END 
  73.  
  74. #endif // __SUPPORT_CCUSERDEFAULT_H__ 

CCUserDefault.cpp:

[cpp] view plaincopyprint?
  1. #include "CCUserDefault.h" 
  2. #include "platform/CCCommon.h" 
  3. #include "platform/CCFileUtils.h" 
  4. #include <libxml/parser.h> 
  5. #include <libxml/tree.h> 
  6.  
  7. // XML的根节点名称 
  8. #define USERDEFAULT_ROOT_NAME    "userDefaultRoot" 
  9. //默认的XML文件名称 
  10. #define XML_FILE_NAME "UserDefault.xml" 
  11. //使用C++标准库的命名空间 
  12. using namespace std; 
  13. //使用Cocos2d命名空间 
  14. NS_CC_BEGIN 
  15. //单例的指针 
  16. static xmlDocPtr g_sharedDoc = NULL; 
  17.  
  18. //静态全局函数,用于取得一个键的XML结点指针 
  19. static xmlNodePtr getXMLNodeForKey(constchar* pKey, xmlNodePtr *rootNode) 
  20.     //定义用于存储返回结果的临时指针变量并置空 
  21.     xmlNodePtr curNode = NULL; 
  22.  
  23.     //键值的有效性判断 
  24.     if (! pKey) 
  25.     { 
  26.         return NULL; 
  27.     } 
  28.  
  29.     do  
  30.     { 
  31.         //取得根结点 
  32.         *rootNode = xmlDocGetRootElement(g_sharedDoc); 
  33.         if (NULL == *rootNode) 
  34.         { 
  35.             CCLOG("read root node error"); 
  36.             break
  37.         } 
  38.  
  39.         //循环查询相应的键结点 
  40.         curNode = (*rootNode)->xmlChildrenNode; 
  41.         while (NULL != curNode) 
  42.         { 
  43.               //如果键结点名称与查询键名称一致中断退出循环 
  44.             if (! xmlStrcmp(curNode->name, BAD_CAST pKey)) 
  45.             { 
  46.                 break
  47.             } 
  48.              //否则指针指向下一个结点继续循环 
  49.             curNode = curNode->next; 
  50.         } 
  51.     } while (0); 
  52.     //返回结点指针 
  53.     return curNode; 
  54. //取得相应的键值 
  55. static inlineconst char* getValueForKey(constchar* pKey) 
  56.     //定义用于存储返回结果的临时字符指针变量并置空 
  57.     const char* ret = NULL; 
  58.     //定义结点指针变量取得相应的键结点。 
  59.     xmlNodePtr rootNode; 
  60.     xmlNodePtr node = getXMLNodeForKey(pKey, &rootNode); 
  61.  
  62.     // 如果找到了相应的结点,取得结点的内存值转换为字符指针返回。 
  63.     if (node) 
  64.     { 
  65.         ret = (const char*)xmlNodeGetContent(node); 
  66.     } 
  67.  
  68.     return ret; 
  69. //设置相应的键值 
  70. static void setValueForKey(constchar* pKey, constchar* pValue) 
  71.     xmlNodePtr rootNode; 
  72.     xmlNodePtr node; 
  73.  
  74.     // 有效性判断 
  75.     if (! pKey || ! pValue) 
  76.     { 
  77.         return
  78.     } 
  79.  
  80.     // 取得相应的键结点 
  81.     node = getXMLNodeForKey(pKey, &rootNode); 
  82.  
  83.     // 如果找到,设置结点的值为pValue 
  84.     if (node) 
  85.     { 
  86.         xmlNodeSetContent(node, BAD_CAST pValue); 
  87.     } 
  88.     else 
  89.     { 
  90.          //如果找不到键值,则生成相应的键结点和键值结点并放入根结点下。 
  91.         if (rootNode) 
  92.         { 
  93.              //先创建键结点。 
  94.             xmlNodePtr tmpNode = xmlNewNode(NULL, BAD_CAST pKey); 
  95.              //再创建健值结点。 
  96.             xmlNodePtr content = xmlNewText(BAD_CAST pValue); 
  97.              //将键点点放到根结点下。 
  98.             xmlAddChild(rootNode, tmpNode); 
  99.              //将键帧结点放到键结点下。 
  100.             xmlAddChild(tmpNode, content); 
  101.         }     
  102.     } 
  103.  
  104. //初始化单例指针置空 
  105. CCUserDefault* CCUserDefault::m_spUserDefault = 0; 
  106. //初始化XML文件路径为空 
  107. string CCUserDefault::m_sFilePath = string(""); 
  108. //初始化文件路径是否被初始化的标记值为false 
  109. bool CCUserDefault::m_sbIsFilePathInitialized =false
  110.  
  111. //析构 
  112. CCUserDefault::~CCUserDefault() 
  113.     //将数据写入文件 
  114.     flush(); 
  115.     //释放相应的XML文件 
  116.     if (g_sharedDoc) 
  117.     { 
  118.         xmlFreeDoc(g_sharedDoc); 
  119.         g_sharedDoc = NULL; 
  120.     } 
  121.     //单例指针置空 
  122.     m_spUserDefault = NULL; 
  123. //构造 
  124. CCUserDefault::CCUserDefault() 
  125.     //读取相应的XML文件。 
  126.     g_sharedDoc = xmlReadFile(getXMLFilePath().c_str(), "utf-8", XML_PARSE_RECOVER); 
  127. //释放单例 
  128. void CCUserDefault::purgeSharedUserDefault() 
  129.     CC_SAFE_DELETE(m_spUserDefault); 
  130.     m_spUserDefault = NULL; 
  131. //从指定的键中取得布尔值 
  132. bool CCUserDefault::getBoolForKey(constchar* pKey) 
  133.      return getBoolForKey(pKey, false); 
  134. //从指定的键中取得布尔值,如果没有则返回默认参数。 
  135. bool CCUserDefault::getBoolForKey(constchar* pKey, bool defaultValue) 
  136.     const char* value = getValueForKey(pKey); 
  137.     bool ret = defaultValue; 
  138.  
  139.     if (value) 
  140.     { 
  141.         ret = (! strcmp(value, "true")); 
  142.         xmlFree((void*)value); 
  143.     } 
  144.  
  145.     return ret; 
  146. //从指定的键中取得整数值 
  147. int CCUserDefault::getIntegerForKey(constchar* pKey) 
  148.     return getIntegerForKey(pKey, 0); 
  149. //从指定的键中取得整数值,如果没有则返回默认参数 
  150. int CCUserDefault::getIntegerForKey(constchar* pKey, int defaultValue) 
  151.     const char* value = getValueForKey(pKey); 
  152.     int ret = defaultValue; 
  153.  
  154.     if (value) 
  155.     { 
  156.         ret = atoi(value); 
  157.         xmlFree((void*)value); 
  158.     } 
  159.  
  160.     return ret; 
  161. //从指定的键中取得浮点值 
  162. float CCUserDefault::getFloatForKey(constchar* pKey) 
  163.     return getFloatForKey(pKey, 0.0f); 
  164. //从指定的键中取得浮点值,如果没有则返回默认参数。 
  165. float CCUserDefault::getFloatForKey(constchar* pKey, float defaultValue) 
  166.     float ret = (float)getDoubleForKey(pKey, (double)defaultValue); 
  167.   
  168.     return ret; 
  169. //从指定的键中取得双精度值 
  170. double  CCUserDefault::getDoubleForKey(constchar* pKey) 
  171.     return getDoubleForKey(pKey, 0.0); 
  172. //从指定的键中取得双精度值,如果没有则返回默认参数。 
  173. double CCUserDefault::getDoubleForKey(constchar* pKey, double defaultValue) 
  174.     const char* value = getValueForKey(pKey); 
  175.     double ret = defaultValue; 
  176.  
  177.     if (value) 
  178.     { 
  179.         ret = atof(value); 
  180.         xmlFree((void*)value); 
  181.     } 
  182.  
  183.     return ret; 
  184. //从指定的键中取得字符串值 
  185. std::string CCUserDefault::getStringForKey(constchar* pKey) 
  186.     return getStringForKey(pKey,""); 
  187. //从指定的键中取得字符串值,如果没有则返回默认参数 
  188. string CCUserDefault::getStringForKey(constchar* pKey, const std::string & defaultValue) 
  189.     const char* value = getValueForKey(pKey); 
  190.     string ret = defaultValue; 
  191.  
  192.     if (value) 
  193.     { 
  194.         ret = string(value); 
  195.         xmlFree((void*)value); 
  196.     } 
  197.  
  198.     return ret; 
  199. //设置指定键的布尔值 
  200. void CCUserDefault::setBoolForKey(constchar* pKey, bool value) 
  201.     // save bool value as string 
  202.  
  203.     if (true == value) 
  204.     { 
  205.         setStringForKey(pKey, "true"); 
  206.     } 
  207.     else 
  208.     { 
  209.         setStringForKey(pKey, "false"); 
  210.     } 
  211. //设置指定键的整数值 
  212. void CCUserDefault::setIntegerForKey(constchar* pKey, int value) 
  213.     // check key 
  214.     if (! pKey) 
  215.     { 
  216.         return
  217.     } 
  218.  
  219.     // format the value 
  220.     char tmp[50]; 
  221.     memset(tmp, 0, 50); 
  222.     sprintf(tmp, "%d", value); 
  223.  
  224.     setValueForKey(pKey, tmp); 
  225. //设置指定键的浮点值 
  226. void CCUserDefault::setFloatForKey(constchar* pKey, float value) 
  227.     setDoubleForKey(pKey, value); 
  228. //设置指定键的双精度值 
  229. void CCUserDefault::setDoubleForKey(constchar* pKey, double value) 
  230.     // check key 
  231.     if (! pKey) 
  232.     { 
  233.         return
  234.     } 
  235.  
  236.     // format the value 
  237.     char tmp[50]; 
  238.     memset(tmp, 0, 50); 
  239.     sprintf(tmp, "%f", value); 
  240.  
  241.     setValueForKey(pKey, tmp); 
  242. //设置指定键的字符串值 
  243. void CCUserDefault::setStringForKey(constchar* pKey, const std::string & value) 
  244.     // check key 
  245.     if (! pKey) 
  246.     { 
  247.         return
  248.     } 
  249.  
  250.     setValueForKey(pKey, value.c_str()); 
  251. //取得单例 
  252. CCUserDefault* CCUserDefault::sharedUserDefault() 
  253.     //初始化XML文件 
  254.     initXMLFilePath(); 
  255.  
  256.     //如果文件不存在则创建,如果创建不成功返回失败。 
  257.     if ((! isXMLFileExist()) && (! createXMLFile())) 
  258.     { 
  259.         return NULL; 
  260.     } 
  261.     //如果当前单例指针为空,创建单例 
  262.     if (! m_spUserDefault) 
  263.     { 
  264.         m_spUserDefault = new CCUserDefault(); 
  265.     } 
  266.     //返回单例指针 
  267.     return m_spUserDefault; 
  268. //XML文件是否存在。 
  269. bool CCUserDefault::isXMLFileExist() 
  270.     //创建一个文件指针打开相应的文件。 
  271.     FILE *fp = fopen(m_sFilePath.c_str(),"r"); 
  272.     bool bRet = false
  273.     //看是否能打开以判断是否存在。 
  274.     if (fp) 
  275.     { 
  276.         bRet = true
  277.         fclose(fp); 
  278.     } 
  279.  
  280.     return bRet; 
  281. //初始化XML文件路径 
  282. void CCUserDefault::initXMLFilePath() 
  283.     //如果初始化的标记为false,组合出文件字符串。 
  284.     if (! m_sbIsFilePathInitialized) 
  285.     { 
  286.         //文件路径名为文件系统的写入路径[后面详解]下的“UserDefault.xml”。 
  287.         m_sFilePath += CCFileUtils::sharedFileUtils()->getWriteablePath() + XML_FILE_NAME; 
  288.         m_sbIsFilePathInitialized = true
  289.     }     
  290.  
  291. //创建XML文件 
  292. bool CCUserDefault::createXMLFile() 
  293.     bool bRet = false
  294.     //定义临时的XML文档指针 
  295.     xmlDocPtr doc = NULL; 
  296.     //使用do-while框架结构来随时中断 
  297.     do  
  298.     { 
  299.         // 创建一个新的1.0版的XML文档 
  300.         doc = xmlNewDoc(BAD_CAST"1.0"); 
  301.         if (doc == NULL) 
  302.         { 
  303.             CCLOG("can not create xml doc"); 
  304.             break
  305.         } 
  306.  
  307.         // 创建根结点 
  308.         xmlNodePtr rootNode = xmlNewNode(NULL, BAD_CAST USERDEFAULT_ROOT_NAME); 
  309.         if (rootNode == NULL) 
  310.         { 
  311.             CCLOG("can not create root node"); 
  312.             break
  313.         } 
  314.  
  315.         //设置创建的结点为XML文档中的根结点 
  316.         xmlDocSetRootElement(doc, rootNode); 
  317.  
  318.         //保存XML文件 
  319.         xmlSaveFile(m_sFilePath.c_str(), doc); 
  320.  
  321.         bRet = true
  322.     } while (0); 
  323.  
  324.     // 释放文档指针 
  325.     if (doc) 
  326.     { 
  327.         xmlFreeDoc(doc); 
  328.     } 
  329.     //返回成败 
  330.     return bRet; 
  331. //取得XML文件路径 
  332. const string& CCUserDefault::getXMLFilePath() 
  333.     return m_sFilePath; 
  334. //立即将XML数据写入文件 
  335. void CCUserDefault::flush() 
  336.     // 如果文档有效则进行保存文档到文件中。 
  337.     if (g_sharedDoc) 
  338.     { 
  339.         xmlSaveFile(CCUserDefault::sharedUserDefault()->getXMLFilePath().c_str(), g_sharedDoc); 
  340.     } 
  341.  
  342. NS_CC_END 

              这引CCUserDefault类写的真是不错。非常简洁好用。但我们要明白“文件系统的写入路径”是什么?

              在CCFileUtils.cpp中找到相应的函数:

//取得文件写入路径

[cpp] view plaincopyprint?
  1. string CCFileUtils::getWriteablePath() 
  2.     // 取得当前程序所在目录 
  3.     char full_path[_MAX_PATH + 1]; 
  4.     ::GetModuleFileNameA(NULL, full_path, _MAX_PATH + 1); 
  5.  
  6.     // 如果是Release模式 
  7. #ifndef _DEBUG 
  8.         // 取得移除路径的文件名 
  9.         char *base_name = strrchr(full_path,'\\'); 
  10.  
  11.         if(base_name) 
  12.         { 
  13.             char app_data_path[_MAX_PATH + 1]; 
  14.  
  15.             // 取得系统文件夹应用程序数据目录,如C:\Documents and Settings\username\Local Settings\Application Data,可参看: http://wenku.baidu.com/view/412cfc02f78a6529647d53e5.html 
  16.  
  17.             if (SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, app_data_path))) 
  18.             { 
  19.                 //创建字符串ret存放取出的路径 
  20.                 string ret((char*)app_data_path); 
  21.  
  22.                 //字符串尾部加上文件名。 
  23.                 ret += base_name; 
  24.  
  25.                 // 去除扩展名并加上”\\” 
  26.                 ret = ret.substr(0, ret.rfind(".")); 
  27.                 ret += "\\"
  28.  
  29.                 // 创建相应的目录 
  30.                 if (SUCCEEDED(SHCreateDirectoryExA(NULL, ret.c_str(), NULL))) 
  31.                 { 
  32.                     //如果成功返回ret。 
  33.                     return ret; 
  34.                 } 
  35.             } 
  36.         } 
  37. #endif // not defined _DEBUG 
  38.  
  39.     //创建字符串ret存放当前程序所在目录。 
  40.     string ret((char*)full_path); 
  41.  
  42.     // 截取带”\\”部分的路径。 
  43.     ret =  ret.substr(0, ret.rfind("\\") + 1); 
  44.     //返回ret。 
  45.     return ret; 

              这个函数对于DEBUG和RELEASE模式有区别处理,DEBUG模式取出的路径即为当前程序所在目录,RELEASE模式则在系统目录下创建当前程序名称的目录并返回。

              接下来我们看一下Cocos2d-x在例子中的具体使用,找到TestCpp中的UserDefaultTest。

UserDefaultTest.h:

[cpp] view plaincopyprint?
  1. <span style="font-size: 14px;">#ifndef _USERDEFAULT_TEST_H_ 
  2. #define _USERDEFAULT_TEST_H_ 
  3.  
  4. #include "cocos2d.h" 
  5. #include "../testBasic.h" 
  6. //创建一个层用于处理XML数据 
  7. class UserDefaultTest : public CCLayer 
  8. public
  9.     UserDefaultTest(); 
  10.     ~UserDefaultTest(); 
  11.  
  12. private
  13.     void doTest(); 
  14. }; 
  15. //演示用的场景 
  16. class UserDefaultTestScene :public TestScene 
  17. public
  18.     virtual void runThisTest(); 
  19. }; 
  20. #endif // _USERDEFAULT_TEST_H_ 
  21. </span> 

对应的CPP:

[cpp] view plaincopyprint?
  1. // 开启COCOS2D的DEBUG标记 
  2. #define COCOS2D_DEBUG 1 
  3. #include "UserDefaultTest.h" 
  4. #include "stdio.h" 
  5. #include "stdlib.h" 
  6. //层的构造函数。 
  7. UserDefaultTest::UserDefaultTest() 
  8.     //取得屏幕大小,创建文字标签提示。 
  9.     CCSize s = CCDirector::sharedDirector()->getWinSize(); 
  10. CCLabelTTF* label = CCLabelTTF::create("CCUserDefault test see log","Arial", 28); 
  11. //将标签放到当前层中并置于屏幕中央。 
  12.     addChild(label, 0); 
  13.     label->setPosition( ccp(s.width/2, s.height-50) ); 
  14.     //调用测试函数。 
  15.     doTest(); 
  16.  
  17. void UserDefaultTest::doTest() 
  18.     //开始打印日志。 
  19.     CCLOG("********************** init value ***********************"); 
  20.  
  21.     // 创建CCUserDefault单例并创建相应的数据类型键,设置其键值。 
  22.  
  23.     CCUserDefault::sharedUserDefault()->setStringForKey("string","value1"); 
  24.     CCUserDefault::sharedUserDefault()->setIntegerForKey("integer", 10); 
  25.     CCUserDefault::sharedUserDefault()->setFloatForKey("float", 2.3f); 
  26.     CCUserDefault::sharedUserDefault()->setDoubleForKey("double", 2.4); 
  27.     CCUserDefault::sharedUserDefault()->setBoolForKey("bool",true); 
  28.  
  29.     // 设置完后,打印各类型键取出的值。 
  30.     string ret = CCUserDefault::sharedUserDefault()->getStringForKey("string"); 
  31.     CCLOG("string is %s", ret.c_str()); 
  32.  
  33.     double d = CCUserDefault::sharedUserDefault()->getDoubleForKey("double"); 
  34.     CCLOG("double is %f", d); 
  35.  
  36.     int i = CCUserDefault::sharedUserDefault()->getIntegerForKey("integer"); 
  37.     CCLOG("integer is %d", i); 
  38.  
  39.     float f = CCUserDefault::sharedUserDefault()->getFloatForKey("float"); 
  40.     CCLOG("float is %f", f); 
  41.  
  42.     bool b = CCUserDefault::sharedUserDefault()->getBoolForKey("bool"); 
  43.     if (b) 
  44.     { 
  45.         CCLOG("bool is true"); 
  46.     } 
  47.     else 
  48.     { 
  49.         CCLOG("bool is false"); 
  50.     } 
  51.      
  52.     //CCUserDefault::sharedUserDefault()->flush(); 
  53.     CCLOG("********************** after change value ***********************"); 
  54.  
  55.     // 改变相应键的键值。 
  56.  
  57.     CCUserDefault::sharedUserDefault()->setStringForKey("string","value2"); 
  58.     CCUserDefault::sharedUserDefault()->setIntegerForKey("integer", 11); 
  59.     CCUserDefault::sharedUserDefault()->setFloatForKey("float", 2.5f); 
  60.     CCUserDefault::sharedUserDefault()->setDoubleForKey("double", 2.6); 
  61.     CCUserDefault::sharedUserDefault()->setBoolForKey("bool",false); 
  62.  
  63.     //将XML数据保存到相应文件中。 
  64.     CCUserDefault::sharedUserDefault()->flush(); 
  65.  
  66.     // 再次打印各键值。 
  67.  
  68.     ret = CCUserDefault::sharedUserDefault()->getStringForKey("string"); 
  69.     CCLOG("string is %s", ret.c_str()); 
  70.  
  71.     d = CCUserDefault::sharedUserDefault()->getDoubleForKey("double"); 
  72.     CCLOG("double is %f", d); 
  73.  
  74.     i = CCUserDefault::sharedUserDefault()->getIntegerForKey("integer"); 
  75.     CCLOG("integer is %d", i); 
  76.  
  77.     f = CCUserDefault::sharedUserDefault()->getFloatForKey("float"); 
  78.     CCLOG("float is %f", f); 
  79.  
  80.     b = CCUserDefault::sharedUserDefault()->getBoolForKey("bool"); 
  81.     if (b) 
  82.     { 
  83.         CCLOG("bool is true"); 
  84.     } 
  85.     else 
  86.     { 
  87.         CCLOG("bool is false"); 
  88.     } 
  89.  
  90. //析构 
  91. UserDefaultTest::~UserDefaultTest() 
  92. //场景启动时调用。 
  93. void UserDefaultTestScene::runThisTest() 
  94.     //创建一个演示用的层并放到当前演示场景中。 
  95.     CCLayer* pLayer = new UserDefaultTest(); 
  96.     addChild(pLayer); 
  97.     //启动当前场景。 
  98.     CCDirector::sharedDirector()->replaceScene(this); 
  99.     pLayer->release(); 

当我们在DEBUG模式下运行此演示后程序截图为:



在程序的运行目录会出现:



              用IE打开后可以看到相应的键值数据。这样我们便学会了如何存储游戏中用到的数据到XML文件中。下课!

0 0
原创粉丝点击