資料本地化
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
開頭
<?xml version=”1.0” encoding=”utf-8”?>
b xml文檔必須有根元素
xml文檔必須有一個元素是所有其他元素的父元素。該元素稱為根元素。
<root>
<child>
<subchild>…..</subchild>
</child>
</root>
所有xml元素都必須有關閉标簽
<p>this is a paragraph</p>
<p>this is another paragraph</p>
d
在xml中,xml的屬性值須加引号。
<note date="08/08/2008">
<to>george</to>
<from>john</from>
</note>
e xml中的注釋
<!— this is a comment -->
xml元素
指的是從(且包括)開始标簽直到(且包括)結束标簽的部分。
元素可包含其他元素、文本或者兩者的混合物。元素也可以擁有屬性。
<bookstore>
<book category="children">
<title>harry potter</title>
<author>j k. rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title>learning xml</title>
<author>erik t. ray</author>
<year>2003</year>
<price>39.95</price>
</bookstore>
例:
<?xml version="1.0" encoding="iso-8859-1"?>
<note>
<heading>reminder</heading>
<body>don't forget the meeting!</body>
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->linkendchild(pdoc->newtext("cocos2d-x"));
arrayelement->linkendchild(elm);
}
pdoc->savefile(filepath.c_str());
pdoc->print();
delete pdoc;
在cocos2d-x-2.2.3\projects\hello\proj.win32\debug.win32下
<?xml version="1.0" encoding="utf-8"?>
<plist version="1.0">
<!--this is xml comment-->
<dic>
<key>text</key>
<array>
<name>cocos2d-x</name>
</array>
</dic>
</plist>
17.3.4解析xml
void parsexml(const char * filename) {
std::string filepath = ccfileutils::sharedfileutils()->getwritablepath() + filename;
xmlerror errorid = pdoc->loadfile(filepath.c_str());
if (errorid != 0) {
格式錯誤
return;
xmlelement *rootele = pdoc->rootelement();
//擷取第一個節點屬性
const xmlattribute *attribute = rootele->firstattribute();
//列印節點屬性名和值
cclog("attribute_name = %s,attribute_value = %s", attribute->name(),
attribute->value());
xmlelement *dicele = rootele->firstchildelement("dic");
xmlelement *keyele = dicele->firstchildelement("key");
if (keyele) {
cclog("keyele text= %s", keyele->gettext());
xmlelement *arrayele = keyele->nextsiblingelement();
xmlelement *childele = arrayele->firstchildelement();
while (childele) {
cclog("childele text= %s", childele->gettext());
childele = childele->nextsiblingelement();
attribute_name = version,attribute_value = 1.0
keyele text= text
childele text= cocos2d-x
userdefault.xml操作案例:
t22userdefault.h
#ifndef
__t22ccuserdefault_h__
#define
#include
"cocos2d.h"
"tback.h"
class
t22ccuserdefault :public
tback
public:
create_func(t22ccuserdefault);
bool
init();
static
ccscene *
scene();
void
makexml(char
* filename);
parsexml(char
};
#endif
t22userdefault.cpp
"t22userdefault.h"
"appmacros.h"
"support/tinyxml2/tinyxml2.h"
using
namespace
tinyxml2;
t22ccuserdefault::scene()
scene =
ccscene::create();
t22ccuserdefault *
layer =
t22ccuserdefault::create();
scene->addchild(layer);
return
scene;
t22ccuserdefault::init()
tback::init();
//通過這種方式向userdefault.xml中寫内容
ccuserdefault::shareduserdefault()->setintegerforkey("integer",
100);
ccuserdefault::shareduserdefault()->setstringforkey("string","oooo");
//獲得xml的路徑
std::string
str =
ccuserdefault::shareduserdefault()->getxmlfilepath();
//列印xml的路徑
cclog("path
= %s",
str.c_str());
cclog("isxmlexist
= %d",
ccuserdefault::shareduserdefault()->isxmlfileexist());
//如果有這個key的值則列印出,如果沒有這個key則傳回預設的值120
int
value =
ccuserdefault::shareduserdefault()->getintegerforkey("integer",
120);
cclog("value
value);
true;
運作結果:
userdefault.xml的内容:
操作xml和解釋xml
makexml("test");
parsexml("test");
t22ccuserdefault::makexml(char
* filename) {
filepath =
ccfileutils::sharedfileutils()->getwritablepath()
+ filename;
tinyxml2::xmldocument
*pdoc =
new
tinyxml2::xmldocument();
xmldeclaration *pdel
= pdoc->newdeclaration("xml
version=\"1.0\" encoding = \"utf-8\"");
//添加 plist
xmlelement *plistelement
= pdoc->newelement("plist");
plistelement->setattribute("version",
"1.0");
xmlcomment *commentelement
= pdoc->newcomment("this
is x comment");
//添加 dic
xmlelement *dicelement
= pdoc->newelement("dic");
//添加 key
xmlelement *keyelement
= pdoc->newelement("key");
xmlelement *arrayelement
= pdoc->newelement("array");
for (int
i = 0;
i < 3;
i++) {
xmlelement *elm
= pdoc->newelement("name");
delete
pdoc;
t22ccuserdefault::parsexml(char
* filename)
xmlerror
errorid =
pdoc->loadfile(filepath.c_str());
if (errorid
!= 0) {
xmlelement *rootele
= pdoc->rootelement();
const
xmlattribute *attribute
= rootele->firstattribute();
cclog("attribute_name
= %s,attribute_value = %s",
attribute->name(),
xmlelement *dicele
= rootele->firstchildelement("dic");
xmlelement *keyele
= dicele->firstchildelement("key");
if (keyele)
cclog("keyele
text= %s",
keyele->gettext());
xmlelement *arrayele
= keyele->nextsiblingelement();
xmlelement *childele
= arrayele->firstchildelement();
while (childele)
cclog("childele
childele->gettext());
childele =
childele->nextsiblingelement();
xml檔案:
ccstring,ccarray,ccdictionary
init()方法中
ccstring *str
= ccstring::create("1234");
cclog("ccstring
str = %s",
str->getcstring());
inttypevalue = %d",
str->intvalue());
資料結構:ccstring
/*使用std::string
建立了一個字元串,
你也可以傳遞一個c
字元串指針,因為
std::string
的構造函數可以通路c
字元串指針
* @傳回的ccstring
指針是一個自動釋放對象,
*也就意味着你不需要調用release
操作,除非你retain
了.
*/
static ccstring* create(const std::string& str);
/*使用格式化方式來建立一個字元串,這個方法和c
語言裡面的‘sprintf’類似,預設
緩存大小是(1024*100)bytes
*假如你想要改變這個緩存大小,你可以去ccstring.cpp
中,更改kmaxstringlen
這個宏定義。
static ccstring* createwithformat(const char* format, …);
/*
使用二進制資料來建立字元串
static ccstring* createwithdata(const unsigned char* pdata, unsigned
long nlen);
/*使用一個檔案來建立一個字元串,
* @return a ccstring pointer which is an autorelease object pointer,
* it means that you needn't do a release operation unless you retain
it.
static ccstring* createwithcontentsoffile(const char* pszfilename);
轉換
ccstring
允許ccstring
執行個體變量轉換為另外類型的變量。
/* convert to int value */
int intvalue() const;
/* convert to unsigned int value */
unsigned int uintvalue() const;
/* convert to float value */
float floatvalue() const;
/* convert to double value */
double doublevalue() const;
/* convert to bool value */
bool boolvalue() const;
常見的宏定義
#define ccstringmake(str) ccstring::create(str)
#define ccs ccstringmake
ccarray
ccarray是一個面向對象包裝類
ccarray繼承至ccobject(ccobject主要是為了自動記憶體管理而建立的)并且提供了一系列接口。
建立
/**
建立一個數組*/
static ccarray* create();
使用一些對象建立數組*/
static ccarray* create(ccobject* pobject, …);
使用一個對象建立數組*/
static ccarray* createwithobject(ccobject* pobject);
建立一個指定大小的數組*/
static ccarray* createwithcapacity(unsigned int capacity);
使用一個現有的ccarray
數組來建立一個數組*/
static ccarray* createwitharray(ccarray* otherarray);
插入
插入一個對象*/
void addobject(ccobject* object);
插入别外一個數組裡面的全部對象*/
void addobjectsfromarray(ccarray* otherarray);
在一個确定的索引位置插入一個對象*/
void insertobject(ccobject* object, unsigned int index);
删除
移除最後的一個對象*/
void removelastobject(bool breleaseobj = true);
/**移除一個确定的對象*/
void removeobject(ccobject* object, bool breleaseobj = true);
移除一個确定索引位置的元素*/
void removeobjectatindex(unsigned int index, bool breleaseobj = true);
移除全部元素*/
void removeobjectsinarray(ccarray* otherarray);
移除所有對象*/
void removeallobjects();
快速移除一個對象*/
void fastremoveobject(ccobject* object);
快速移除一個确定索引位置的對象*/
void fastremoveobjectatindex(unsigned int index);
remove
和fastremove
有什麼差別,可以看看源代碼,remove
是從ccarray中完全的移除,fastremove
隻是将ccarray
中對應的對象釋放掉了,沒夠改變整個ccarray
的結構。從代碼上來看,差別在于删除元素之後,是否把數組之後的元素向前移動覆寫掉之前位置的元素。代碼上的差别如下所示:
unsigned int remaining = arr->num - index;
if(remaining>0)
memmove((void *)&arr->arr[index], (void *)&arr->arr[index+1],
remaining * sizeof(ccobject*));
周遊
ccarray_foreach(arr, obj)
主要事項
一般不會被增加到其他類中,是以他的引用計數是1,并且設定為autorelease
對象。建立ccarray
對象并且retain,然後在這個類中的析構函數中調用release
方法來釋放記憶體。
如果ccobject
對象添加到ccarray
中,那麼ccobject
對象的引用計數将
會加1.
ccdictionary
ccdirtionary使用uthash實作的
關鍵字類型
ccdictionary支援兩種類型的關鍵字,一個是std::string,一個是int.一個ccdictionary執行個體對象隻支援唯一的關鍵字。是以在你調用”setobject”方法的時候,你需要确認一下。
static ccdictionary * create();
static ccdictionary * createwithdictionary(ccdictionary *srcdict)
static ccdictionary * createwithcontentsofffile(const char * pfilename);
相同key的value會發生覆寫行為。
void setobject(ccobject *pobject,const std::string &key);
void setobject(ccobject *pobject,intptr_t key);
void removeobjectforkey (const std::string &key)
void removeobjectforkey (intptr_t key)
void removeobjectsforkeys (ccarray *pkeyarray)
void removeobjectforelememt (ccdictelement *pelement)
void removeallobjects ()
實作了ccdict_foreach
方法來周遊整個字典。而且使用ccdict_foreach
的方式和使用ccarray_foreach
的方式非常類似
ccdictelement* pelement = null;
ccdict_foreach(thedict, pelement)
ccobjectsubclass* psubclassobj = (ccobjectsubclass*)pelement->getobject();
//
你也可以得到目前key,但是你需要确定key
的類型。
std::string onestrkey = pelement->getstrkey(); //
假如key
的類型是string
// int oneintkey = pelement->getintkey(); //
假如有key
的類型是integer
下面就可以使用上面psubclassobj
對象做一些操作了
舉例
// create a dictionary, return an autorelease object.
ccdictionary* pdict = ccdictionary::create();
// insert objects to dictionary
ccstring* pvalue1 = ccstring::create("100");
ccstring* pvalue2 = ccstring::create("120");
ccinteger* pvalue3 = ccinteger::create(200);
pdict->setobject(pvalue1, "key1");
pdict->setobject(pvalue2, "key2");
pdict->setobject(pvalue3, "key3");
// get the object for key
ccstring* pstr1 = (ccstring*)pdict->objectforkey("key1");
cclog("{ key1: %s }", pstr1->getcstring());
ccinteger* pinteger = (ccinteger*)pdict->objectforkey("key3");
cclog("{ key3: %d }", pinteger->getvalue());
結合xml支援中文
ccdictionary * dic = ccdictionary::createwithcontentsoffile("chinese.xml");
ccstring * str = (ccstring *)dic->objectforkey("people1");
cclabelttf * ttf = cclabelttf::create(str->getcstring(), "arial",20);
ttf->setposition(ccp(240, 160));
addchild(ttf);
return true;
案例:
ccdictionary *
dic =
ccdictionary::create();
dic->retain();
ccstring *value1
= ccstring::create("100");
ccstring *value2
= ccstring::create("200");
//第一個是value,第二個是key的意思
dic->setobject(value1,
"key1");
dic->setobject(value2,
"key2");
ccstring *
str2 = (ccstring
*)dic->objectforkey("key1");
//運作結果100
cclog("%d",
str2->intvalue());
cclabelttf *
ttf =
cclabelttf::create(str2->getcstring(),
"courier new", 30);
ttf->setposition(ccp(240,160));