from:://http://www.cnblogs.com/sunguangran/archive/2013/07/29/3222660.html
將顯示文本單獨保存為文本文件
在cocos2d-x的示例項目中有關於配置文件讀取的示例項目,有興趣的童鞋可以自己去找下,這里將示例內容簡化進行簡要介紹。
首先,相關配置文件必須放在項目Resource目錄下,可自己設置二級目錄,方便管理,
strings.plist內容如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>data</key> <dict> <key>hello</key> <string>完美世界,完美生活</string> <key>cocos2d.x.display_fps</key> <true/> <key>cocos2d.x.gl.projection</key> <string>3d</string> <key>cocos2d.x.texture.pixel_format_for_png</key> <string>rgba8888</string> <key>cocos2d.x.texture.pvrv2_has_alpha_premultiplied</key> <false/> </dict> <key>metadata</key> <dict> <key>format</key> <integer>1</integer> </dict> </dict> </plist>
之后我們在自己的cocos2d-x項目導演類實例生成之前需要調用下面代碼進行初始化操作:
CCConfiguration::sharedConfiguration()->loadConfigFile("config/strings.plist");
一般我們在 AppDelegate::applicationDidFinishLaunching() 起始執行該部操作。之后就可以在程序中使用配置文件中的內容了:
CCConfiguration *conf = CCConfiguration::sharedConfiguration(); const char *helloStr = conf->getCString("hello", "unknown"); CCLabelTTF* pLabel = CCLabelTTF::create(helloStr, "Arial", 24); pLabel->setPosition(ccp(origin.x + visibleSize.width/2, origin.y + visibleSize.height - pLabel->getContentSize().height)); this->addChild(pLabel, 1);
這樣,就可以將 strings.plist 中的hello項讀取出現顯示,
分類:
cocos2d-x