一、Cocos2d-x中的聲音
Cocos2d-iphone中包含CocosDenshion庫,從低到高提供三層介面:CDSoundEngine、CDAudioManager和SimpleAudioEngine,但整個庫完全依賴於OpenAL來實現。由於在其他平台上無法提供CocosDenshiono的底層支援,所以只採用了最上層的SimpleAudioEngine類來實現跨平台的聲音引擎,在使用上是十分簡便的。
查看SimpleAudioEngine這個檔案就可以知道其中的播放背景音樂和音效的API了。(檔案位置如:)
註:在Cocos2dx引擎檔案夾中的samples/Cpp/TestCpp/proj.ios 檔案目錄下有一個附帶的例子,裡麵包含了絕大部分的例子介紹,很詳細,是學習Cocos2dx的一個好demo集錦。
關於在Cocos2dx中SimpleAudioEngine聲音引擎的使用例子----CocosDenshionTest
(1)關於音效檔的預先載入,可以使得程式在遊戲中的執行效率提高,但是不可以避免的也會增加記憶體的佔用。
// preload background music and effect SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic( MUSIC_FILE ); SimpleAudioEngine::sharedEngine()->preloadEffect( EFFECT_FILE );
其中的參數是音效檔的名稱即可。查看這兩個預先載入方法可知,這些方法的實現中會協助我們擷取音效檔的全路徑。
void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath){ // Changing file path to full path std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath); static_preloadBackgroundMusic(fullPath.c_str());}void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop){ // Changing file path to full path std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath); static_playBackgroundMusic(fullPath.c_str(), bLoop);}
(2)注意在onExit函數中結束對SimpleAudioEngine的使用
void CocosDenshionTest::onExit(){ CCLayer::onExit(); SimpleAudioEngine::sharedEngine()->end();}
二、Cocos2d-x中的遊戲存檔
在Cocos2d-x中支援遊戲存檔類CCUserDefault可以用作為一個輕量級的資料庫來使用。
(1)其檔案位置為:libs/cocos2dx/support/user_default
關於在Cocos2dx中CCUserDefault的使用例子----CCUserDefaultTest
(2)這個類檔案比較簡單,而且是個單例類使用時,要用sharedUserDefault方法擷取單例對象。
(3)其中主要的API都是一些set和get資料的方法。
例如:
/** @brief Get bool value by key, if the key doesn't exist, a default value will return. You can set the default value, or it is false. */ bool getBoolForKey(const char* pKey); bool getBoolForKey(const char* pKey, bool defaultValue);
/** @brief Set bool value by key. */ void setBoolForKey(const char* pKey, bool value);
(4)set完資料之後,要調用flush這個方法進行儲存。通過查看這個方法的實現
void CCUserDefault::flush(){ [[NSUserDefaults standardUserDefaults] synchronize];}
我們就知道,其實這個CCUserDefault就是OC中的NSUserDefault啦。
(5)注意:使用CCUserDefault這個類儲存資料是將資料存檔到 XML 檔案中的--這隻是針對在cocos2dx 2.1.2 版本以前而言的(why?)
但是我目前安裝的版本是2.1.4版本,其標頭檔中提供了以下這幾個方法:
static void purgeSharedUserDefault(); const static std::string& getXMLFilePath(); static bool isXMLFileExist();
我一看,顯然是用於擷取xml路徑的,我就試了一下:
CCUserDefault::sharedUserDefault()->setStringForKey("key", "value"); CCUserDefault::sharedUserDefault()->flush(); std::string myString = CCUserDefault::sharedUserDefault()->getStringForKey("key"); CCLog("value = %s",myString.c_str()); std::string str = CCUserDefault::getXMLFilePath(); CCLog("xml path_1 = %s",str.c_str()); bool isExit = CCUserDefault::isXMLFileExist(); if (isExit) { CCLog("xml path_2 = %s",str.c_str()); }
終端輸出結果如下:
Cocos2d: value = valueCocos2d: xml path_1 = /Users/ios/Library/Application Support/iPhone Simulator/6.1/Applications/A77F61F5-F763-499D-9505-E938091011EE/Library/Caches/UserDefault.xml
顯然沒有輸出:xml path_2 那麼就是表明XML檔案不存在!!
怎麼可能?我就根據xml path_1的路徑到沙箱中查看Caches目錄下,果然沒有!!
但是假如我們把第一二行代碼刪掉,直接根據key擷取value值,發現也是有資料的。
但是在Preferences目錄下,有一個plist檔案是儲存了這些資料。
我找了好一會兒,終於有點發現了!!
在這個類是實現檔案中的 initXMLFilePath 這個方法實現中
void CCUserDefault::initXMLFilePath(){#ifdef KEEP_COMPATABILITY if (! m_sbIsFilePathInitialized) { // xml file is stored in cache directory before 2.1.2 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; m_sFilePath = [documentsDirectory UTF8String]; m_sFilePath.append("/"); m_sFilePath += XML_FILE_NAME; m_sbIsFilePathInitialized = true; }#endif}
// xml file is stored in cache directory before 2.1.2 原來如此呀!是在2.1.2版本以前是儲存到XML,但是我目前的版本是2.1.4,所以找不到XML,所以會找不到XML 檔案。坑爹呀!個人認為,我目前的版本是 CCUserDefault 這個類的資料是儲存到Preferences目錄下的plist檔案下的。
(6)還有一個問題:在這個類中提供了set增加某一資料項目的方法,但是並沒有提供remove某一個資料項目的方法,所以只能增加不能減少。
關於其中的 static
void purgeSharedUserDefault(); 顯然這個方法是將這個單例對象清除的。查看這個方法的實現
void CCUserDefault::purgeSharedUserDefault(){ m_spUserDefault = NULL;}
(7)總結:關於這個類 CCUserDefault的使用十分的簡單,提供的儲存資料也有限(bool,int , float, double ,string)。