CCDictionary檔案讀取

2021-08-27 13:15:26 字數 4435 閱讀 1113

這些天被cocos2dx如何讀取xml困惑著,現在總結總結,如有錯誤,歡迎指正!

先新建乙個cocos2dx的工程

helloworldscene.cpp中的**如下:

[cpp]view plain

copy

print?

#include"helloworldscene.h"

using

namespace

cocos2d;

ccscene*helloworld::scene()

while

(0);

//returnthescene

return

scene; }

//on"init"youneedtoinitializeyourinstance

bool

helloworld::init()

/*第三層下面說的是array中包含string

special_3

45.0

343.444*/

ccarray*parray3=new

ccarray();

parray3->retain();

parray3=(ccarray*)pdict_2->objectforkey("special_3");

for(

inti=0;icount();i++)

/*第三層

special_4

多***/

cclog("pdict_2:%s"

,((ccstring*)pdict_2->valueforkey(

"special_4"

))->getcstring());

/*第三層

special_5*/

cclog("pdict_2:%s"

,((ccstring*)pdict_2->valueforkey(

"special_5"

))->getcstring());

cclog("strings.xmlcounts:%d"

,pdict->count());

cclog("pdict:%s"

,pdict);

ccarray*parray=new

ccarray();

parray=pdict->allkeys();//把所有的鍵值付給parray

for(int

i=0;icount();i++)

cclabelttf*plabel=cclabelttf::create(((ccstring*)pdict->objectforkey("chinese1"

))->getcstring(),

"arial"

,24);

/*cclabelttf*plabel=cclabelttf::create(((ccstring*)parray->objectatindex(3))->getcstring(),"arial",24);*/

//cclabelttf*plabel=cclabelttf::create("helloworld","arial",24);

cc_break_if(!plabel);

//getwindowsizeandplacethelabelupper.

ccsizesize=ccdirector::shareddirector()->getwinsize();

plabel->setposition(ccp(size.width/2,size.height-50));

//addthelabeltohelloworldlayerasachildlayer.

this

->addchild(plabel,1);

ccsprite*psprite=ccsprite::create("helloworld.png");

cc_break_if(!psprite);

//placethespriteonthecenterofthescreen

psprite->setposition(ccp(size.width/2,size.height/2));

//addthespritetohelloworldlayerasachildlayer.

this

->addchild(psprite,0);

bret=true;

}while

(0);

return

bret; }

void

helloworld::menuclosecallback(ccobject*psender)

strings.xml**如下:

[html]view plain

copy

print?

<?

xmlversion

="1.0"

encoding

="utf-8"

?>

<

plist

version

="1.0"

>

<

dict

>

<

key>

special

key>

<

string

>

<

dict

>

<

key>

special_1

key>

<

string

>

hhhhh

string

>

<

string

>

comeontom

string

>

<

true

>

true

>

<

key>

special_2

key>

<

array

>

<

string

>

comeontom1

string

>

<

string

>

comeontom2

string

>

<

string

>

comeontom3

string

>

array

>

<

key>

special_3

key>

<

array

>

<

integer

>

45.0

integer

>

<

integer

>

3integer

>

<

integer

>

43.444

integer

>

array

>

<

key>

special_4

key>

<

real

>

多**real

>

<

key>

special_5

key>

<

false

>

false

>

dict

>

string

>

<

key>

chinese1

key>

<

string

>

美好的一天

string

>

<

key>

japanese

key>

<

string

>

良い一日を

string

>

<

key>

spanish

key>

<

string

>

buendía

string

>

dict

>

plist

>

最後來個總結

當然了,有興趣看原始碼的同學,可以看這個檔案:ccfileutilscommon_cpp.h(位置:cocos2dx\platform)

C 檔案讀(逐行讀 逐單詞讀)

include include include include include using namespace std int main ifile.clear ifile.seekg 0 檔案重定位 while ifile word coutvector iterator it text.begi...

檔案 讀操作

讀檔案 開啟檔案方式 ios in 輸入流 ifstream 讀取檔案有 四種 操作 include using namespace std include include 標頭檔案的包含 文字檔案 讀檔案 void test01 4 讀資料 第一種 char buf 1024 while ifs ...

Python讀檔案 寫檔案

讀檔案 在相應的資料夾下建立乙個list.txt檔案。建立乙個包含檔案各行內容的列表 將要讀取的檔案的名稱儲存在變數filename中 filename list.txt with open filename as file obj 呼叫open 將乙個表示檔案及其內容的物件儲存到了變數file o...