数据本地化
A CCUserDefault
系统会在默认路径cocos2d-x-2.2.3\projects\Hello\proj.win32\Debug.win32下生成一个名为UserDefault.xml.全部的key皆为char
*型。value类型为bool intfloat double std::string.
读操作
bool getBoolForKey(const char* pKey);
bool getBoolForKey(const char* pKey, bool defaultValue);
int getIntegerForKey(const char* pKey);
int getIntegerForKey(const char* pKey, int defaultValue);
float getFloatForKey(const char* pKey);
float getFloatForKey(const char* pKey, float defaultValue);
double getDoubleForKey(const char* pKey);
double getDoubleForKey(const char* pKey, double defaultValue);
std::string getStringForKey(const char * pKey);
std::string getStringForKey(const char* pKey,const std::string &defaultValue);
对于没有defaultValue的get方法,假设文件里没有对应的key,则得到的是0,假设有则对应的值。
对于有defaultValue的get方法,假设文件里没有对应的key,则得到的是defaultValue,假设有。则返回文件里的对应的值。
B写操作
void setBoolForKey(const char* pKey, bool value);
void setIntegerForKey(const char* pKey, int value);
void setFloatForKey(const char* pKey, float value);
void setDoubleForKey(const char* pKey, double value);
void setStringForKey(const char* pKey, const std::string & value);
Set方法有个特点。是对于相对的key后面会对前面产生覆盖效果。
C写入文件
CCUserDefault::sharedUserDefault()->flush();
尽管window平台是空,可是因为跨平台所导致的。
2 Xml文档格式
简单介绍
XML被设计用来传输和存储数据
语法
A开头
B XML文档必须有根元素
XML文档必须有一个元素是全部其它元素的父元素。该元素称为根元素。
…..
C全部XML元素都必须有关闭标签
This is a paragraph
This is another paragraph
D在XML中,XML的属性值须加引號。
George
John
E XML中的凝视
XML元素
指的是从(且包含)開始标签直到(且包含)结束标签的部分。
元素可包括其它元素、文本或者两者的混合物。元素也能够拥有属性。
Harry PotterJ K. Rowling
2005
29.99
Learning XMLErik T. Ray
2003
39.95
例:
xml version="1.0" encoding="ISO-8859-1"?>
George
John
Reminder
F:生成xml文档
头文件
#include "support/tinyxml2/tinyxml2.h"
using namespace tinyxml2;
void makeXML(const char * fileName)
{
std::string filePath =
CCFileUtils::sharedFileUtils()->getWritablePath() + fileName;
tinyxml2::XMLDocument *pDoc = new tinyxml2::XMLDocument();
//xml声明(參数可选)
XMLDeclaration *pDel = pDoc->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");
pDoc->LinkEndChild(pDel);
//加入plist节点
XMLElement *plistElement = pDoc->NewElement("plist");
plistElement->SetAttribute("version", "1.0");
pDoc->LinkEndChild(plistElement);
XMLComment *commentElement = pDoc->NewComment("this is xml comment");
plistElement->LinkEndChild(commentElement);
//加入dic节点
XMLElement *dicElement = pDoc->NewElement("dic");
plistElement->LinkEndChild(dicElement);
//加入key节点
XMLElement *keyElement = pDoc->NewElement("key");
keyElement->LinkEndChild(pDoc->NewText("Text"));
dicElement->LinkEndChild(keyElement);
XMLElement *arrayElement = pDoc->NewElement("array");
dicElement->LinkEndChild(arrayElement);
for (int i = 0; i < 3; i++) {
XMLElement *elm = pDoc->NewElement("name");
elm->L