Cocos数据篇[3.4](4) ――plist文件操作
【唠叨】 在Cocos中,plist文件是非常常见的配置文件。它是特定格式的xml文件。 例如:小图打包成大图的纹理图片、制作粒子特效、帧动画等,都用到了plist文件作为配置文件。 本节要介绍的是:如何创建plist文件,以及读取plist文件中的数据信息。 【扩展阅读】 http://zh.wikipedia.org/wiki/Plist(维基百科) http://zengrong.net/post/1981.htm(COCOS2D-X中的PLIST文件格式详解) http://cn.cocos2d-x.org/tutorial/show?id=2117(Spritesheet的plist文件格式解析) 【plist文件】 属性列表(Property List)文件是一种用来存储序列化后的对象的文件。 属性列表文件的文件扩展名为 .plist,因此通常被称为plist文件。 1、plist文件在Cocos中的应用 (1)图片纹理的配置信息 将多个纹理小图片打包成一个大图片,并生成plist文件。用于配置各个小图的名称、尺寸大小、以及在大图中的所在的矩形区域位置等信息。 可以使用TexturePacker工具,将多个小碎图的纹理打包成一张大图片。 (2)帧动画的配置信息 将帧动画的数据信息,生成为plist配置文件。包含每帧间隔、动画重复次数、每一帧所需的图片、每张图片的名称、尺寸大小、以及在大图中所在的矩形区域位置等信息。 (3)粒子特效的配置信息 将粒子特效的数据信息,生成为plist配置文件。包含粒子发射器的位置信息、发射器模式、最大粒子数量、发射角度、发射速度、纹理贴图等等信息。 (4)还有其它。 2、plist文件格式 plist文件为属性列表文件,类似于键值对(key-value)的形式。 plist文件举例: // <?xmlversion="1.0"encoding="UTF-8"?> <!DOCTYPEplistPUBLIC"-//Apple//DTDPLIST1.0//EN""http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plistversion="1.0"> <dict> <key>dict</key> <dict> <key>name</key> <string>Alice</string> <key>age</key> <string>20</string> </dict> <key>array</key> <array> <integer>0</integer> <integer>1</integer> <integer>2</integer> </array> <key>bool</key> <true/> <key>data</key> <data></data> <key>date</key> <date>2015-02-16T16:47:11Z</date> <key>number</key> <integer>123456</integer> <key>string</key> <string>helloworld!</string> </dict> </plist> // 属性类型有: > Dictionary :字典。(子属性列表为:键值对形式) > Array :数组。(子属性列表为:数组值的形式) > Boolean :逻辑值。(true / false) > Number :数字。 > String :字符串。 > Date :日期。 > Data :数据。 其中,根节点只能为字典或数组。 并且在字典或数组中,键对应的值依然可以为以上的各个属性类型。 3、创建/编辑plist文件 在Mac OS系统中,XCode可以直接创建和编辑plist文件。 当然也可以使用plist编辑软件,或直接使用文本编辑器进行编写。 XCode中,编辑plist文件非常方便。 其中,根节点Root,只能为Dictionary、或Array类型。 以上plist文件数据,代码形式如下: // <?xmlversion="1.0"encoding="UTF-8"?> <!DOCTYPEplistPUBLIC"-//Apple//DTDPLIST1.0//EN""http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plistversion="1.0"> <dict> <key>dict</key> <dict> <key>name</key> <string>Alice</string> <key>age</key> <string>20</string> </dict> <key>array</key> <array> <integer>0</integer> <integer>1</integer> <integer>2</integer> </array> <key>bool</key> <true/> <key>data</key> <data></data> <key>date</key> <date>2015-02-16T16:47:11Z</date> <key>number</key> <integer>123456</integer> <key>string</key> <string>helloworld!</string> </dict> </plist> // 我想大家应该能读得懂把。 4、读取plist文件 接下来讲讲如何读取plist文件的数据信息。 (1)根节点为Dictionary :使用FileUtils::getInstance()->getValueMapFromFile(); 读取为一个ValueMap 。 (2)根节点为Array :使用FileUtils::getInstance()->getValueVectorFromFile(); 读取为一个ValueVector 。 使用举例: // //文件路径 std::stringpath="/soft/cocos2d-x-3.4/projects/Demo34/Resources/testPlist.plist"; //读取plist文件 //以根节点为字典Dictionary为例 //根节点为字典Dictionary,读取为一个ValueMap ValueMapplist=FileUtils::getInstance()->getValueMapFromFile(path); //若根节点为数组Array,读取为一个ValueVector //ValueVectorplist=FileUtils::getInstance()->getValueVectorFromFile(path); //获取数据 //读取"string" CCLOG("string=%s",(plist["string"].asString()).c_str()); //读取"dict",也是一个字典ValueMap ValueMap&dict=plist["dict"].asValueMap(); CCLOG("name=%s",(dict["name"].asString()).c_str()); CCLOG("age=%s",(dict["age"].asString()).c_str()); //读取"array",是一个数组ValueVector ValueVector&array=plist["array"].asValueVector(); for(inti=0;i<array.size();i++){ Value&value=array[i]; CCLOG("%d",value.asInt()); } // (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |