cocos2dx 檔案處理,cocos2dx

來源:互聯網
上載者:User

cocos2dx 檔案處理,cocos2dx

問題1:fopen

在vs下使用fopen進行檔案處理,跑通了,但是移植到android源碼下時就出現了一大推問題,首先需要理解的是在vs下開發資源是存放在執行檔案的相同目錄下的,而移植到android下時資源是存放在assets目錄下的,之前嘗試過使用以下的方法:

 

[cpp] view plaincopy 
  1. char* fileName = "bg.png";  
  2. string filepath = CCFileUtils::sharedFileUtils()->fullPathForFilename(fileName);  
  3. FILE *fp = fopen(filepath.c_str(), "r");  


結果還是報錯,一進遊戲就掛了,估計是跟底層有關,沒有深究,解決方案是使用cocos2dx的CCFileUtils

 

 

[cpp] view plaincopy 
  1. char* filename = "bg.png";  
  2. //獲得檔案在系統的絕對路徑--assets/bg.png  
  3. string filepath = CCFileUtils::sharedFileUtils()->fullPathForFilename(filename);  
  4. //讀取的位元組數,讀取失敗則為0  
  5. unsigned long len = 0;  
  6. //讀取的內容  
  7. unsigned char *data =CCFileUtils::sharedFileUtils()->getFileData(filepath.c_str(), "rb", &len);  
  8.   
  9. /* 
  10. 此處進行你對檔案的操作 
  11. */  
  12.   
  13. //最後記得釋放記憶體  
  14. if(len >0 && data) delete[] data;  
  15.   
  16. /***********************介面說明***************************** 
  17. static unsigned char* getFileData  ( const char *  pszFileName,   
  18.   const char *  pszMode,   
  19.   unsigned long *  pSize   
  20.  )  
  21. pszMode: 
  22. r 以唯讀方式開啟檔案,該檔案必須存在。   
  23. r+ 以可讀寫方式開啟檔案,該檔案必須存在。   
  24. rb+ 讀寫開啟一個二進位檔案,允許讀資料。   
  25. rt+ 讀寫開啟一個文字檔,允許讀和寫。   
  26. w 開啟唯寫檔案,若檔案存在則檔案長度清為0,即該檔案內容會消失。若檔案不存在則建立該檔案。   
  27. w+ 開啟可讀寫檔案,若檔案存在則檔案長度清為零,即該檔案內容會消失。若檔案不存在則建立該檔案。   
  28. a 以附加的方式開啟唯寫檔案。若檔案不存在,則會建立該檔案,如果檔案存在,寫入的資料會被加到檔案尾,即檔案原先的內容會被保留。(EOF符保留)   
  29. a+ 以附加方式開啟可讀寫的檔案。若檔案不存在,則會建立該檔案,如果檔案存在,寫入的資料會被加到檔案尾後,即檔案原先的內容會被保留。 (原來的EOF符不保留)   
  30. wb 唯寫開啟或建立一個二進位檔案;只允許寫資料。   
  31. wb+ 讀寫開啟或建立一個二進位檔案,允許讀和寫。   
  32. wt+ 讀寫開啟或著建立一個文字檔;允許讀寫。   
  33. at+ 讀寫開啟一個文字檔,允許讀或在文本末追加資料。   
  34. ab+ 讀寫開啟一個二進位檔案,允許讀或在檔案末追加資料。 
  35. */  


問題2:tinyxml

 

使用tinyxml來讀xml檔案,之前我的做法是:

 

[cpp] view plaincopy 
  1. //根據xml資料建立動畫  
  2. string xmlFile_path = "bg.xml";  
  3. //讀取動作XML檔案  
  4. tinyxml2::XMLDocument* doc=new tinyxml2::XMLDocument();  
  5. doc->LoadFile(xmlFile_path.c_str());  
  6. tinyxml2::XMLElement *ani_node=doc->RootElement();   
  7. tinyxml2::XMLElement *action_node=ani_node->FirstChildElement("action");  


在vs下是跑通了,但移植到android下就崩潰了,後來在網上看到一些解決方案:串連http://blog.sina.com.cn/s/blog_6e5f7d400100zlj7.html

 

先把xml檔案讀取到一個buff,然後讓tinyxml來解析這個buff,讓tinyxml繞過了讀取檔案失敗的bug

 

[cpp] view plaincopy 
  1. //根據xml資料建立動畫  
  2. string xmlFile_path = "bg.xml";  
  3. //獲得檔案在系統的絕對路徑  
  4. string xmlfilepath = CCFileUtils::sharedFileUtils()->fullPathForFilename(xmlFile_path.c_str());  
  5. //讀取的位元組數,讀取失敗則為0  
  6. unsigned long len = 0;  
  7. //讀取的內容  
  8. unsigned char *data =CCFileUtils::sharedFileUtils()->getFileData(xmlfilepath.c_str(), "r", &len);  
  9. //讀取動作XML檔案  
  10. tinyxml2::XMLDocument* doc=new tinyxml2::XMLDocument();  
  11. doc->Parse((char*)data, len);  
  12. tinyxml2::XMLElement *ani_node=doc->RootElement();   
  13. tinyxml2::XMLElement *action_node=ani_node->FirstChildElement("action");  

cocos2dx 檔案處理由源碼搜藏網整理,轉載請註明出處http://www.codesocang.com/jiaocheng/shoujikaifa/10121.html


cocos2dx CCFileUtils 或者CCDictionary怎讀取檔案

樓主是要載入音樂和圖片資源的時候用到CCFileUtils麼,我最近也碰到類似的比如載入音樂以前是用CCFileUtils 加入音樂檔案SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(CCFileUtils::fullPathFromRelativeFile("Sounds/BGM/Play_bgm_long.mp3"));現在直接改成絕對路徑就行了SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic("Sounds/BGM/Play_bgm_long.mp3");希望樓主碰到的情況跟我的一樣把,其實只要仔細看調用的方法的參數需要什麼樣的對象,應該能夠很大程度上找到原因的
 
cocos2dx 怎得到某個檔案夾下所有的檔案名稱

樓主你好 資訊僅供您參考
using System.IO;

String Path="Images";
List<string> files = null;
string getFilesFilter = "*.jpg;*.jpeg;*.jpe;*.gif;*.bmp;*.png;";
string[] arrFilter = getFilesFilter.Split(';');
if (!string.IsNullOrEmpty(Path))
{
files = new List<string>();
try
{
DirectoryInfo di = new DirectoryInfo("Images");
for (int i = 0; i < arrFilter.Length; i++)
{
if (di.Exists)
{
foreach (FileInfo fileInfo in di.GetFiles(arrFilter[i]))
{
files.Add(fileInfo.FullName);
if (files.Count > 50)
break;
}
}
}
}
catch (IOException) { }
catch (ArgumentException) { }
catch (SecurityException) { }
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.