0
PLIST文件在COCOS2D-X中应用的格式详解
1. 什么是plist文件格式?
这是一种人类可读的串行化对象文件,由苹果公司发明,最早用于NeXTSTEP系统。详情看这里:Plist 。
cocos2d-x从 cocos2d-iphone 发展而来,因此在引擎中大量使用了这种文件格式。
2. 如何编辑plist文件?
在Mac OS X 系统上,XCode 就可以直接打开和编辑plist文件。而在Windows上,我还没有找到可用的plist编辑软件。
当然,plist是基于XML的纯文本格式,随便找个文本编辑器就可以编辑了。
3.cocos2d-x在哪些地方使用了plist格式?
大致有这样几种:
- 图像纹理定义文件
将多个纹理拼在一张大图上,使用 CCSpriteFrameCache 可以载入这类plist文件;
这里有一个图像纹理定义文件的范例: [cocos2d-x]samplesCppTestCppResourcesanimationsgrossini_family.plist 。 - Label纹理定义文件
作用与图像纹理定义文件类似,只不过处理的是自己,面向CCLabelAtlas;
这里有一个Label纹理定义文件的范例: [cocos2d-x]samplesCppTestCppResourcesfontstuffy_bold_italic-charmap.plist 。 - 帧动画定义
定义一个或多个动画中,使用哪些纹理,使用CCAnimationCache 可以载入这类plist文件;
这里有一个帧动画定义文件的范例: [cocos2d-x]samplesCppTestCppResourcesanimationsanimations.plist 。4. 生成plist文件的工具
对于纹理定义文件来说,它的作用是如何在大图中找到碎图的坐标。因此很多拼合碎图的软件可以在拼合碎图的同时生成plist文件。
- TexturePacker 是所有平台上最好用的工具了;
- Zwoptex 是MAC Only的软件,我不太喜欢用;
- SpritePacker 是Windows Only的软件,功能尚可。
5. 图像纹理定义文件格式说明
cocos2d-x中的纹理定义格式,是以Zwoptex生成的格式为标准的。
Zwoptex生成的格式,有4种主要不同的版本:
- format值为0,代表Flash版本;
- format值为1,Zwoptex 0.4b以前支持;
- format值为2,Zwoptex 1.0以后支持,与format1的区别在于支持旋转;
- format值为3,属性名称进行了大幅修改,Zwoptes1.0.2之后支持。这3种格式的plist文件,cocos2d-x都能支持,具体的解析代码在CCSpriteFrameCache::addSpriteFramesWithDictionary。
TexturePacker生成的for cocos2d plist格式与Zwoptex生成的format为2的格式相同
5.1 format为0的plist文件
这里贴一个比较完整plist文件,为了方便描述,其中仅包含一个frame。
1234567891011121314151617181920212223242526272829303132333435<?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>texture</key><dict><key>width</key><integer>256</integer><key>height</key><integer>128</integer></dict><key>frames</key><dict><key>grossini.png</key><dict><key>x</key><integer>103</integer><key>y</key><integer>1</integer><key>width</key><integer>51</integer><key>height</key><integer>109</integer><key>offsetX</key><real>0</real><key>offsetY</key><real>-1</real><key>originalWidth</key><integer>85</integer><key>originalHeight</key><integer>121</integer></dict></dict></dict></plist>5.2 format为2的plist文件内容
1234567891011121314151617181920212223242526272829303132333435<?xml version=”1.0″ encoding=”UTF-8″?><!DOCTYPE plist PUBLIC “-//Apple Computer//DTD PLIST 1.0//EN” “http://www.apple.com/DTDs/PropertyList-1.0.dtd”><plist version=”1.0″><dict><key>frames</key><dict><key>parts-dragon_leg_l.png</key><dict><key>frame</key><string>{{866,2},{152,254}}</string><key>offset</key><string>{0,0}</string><key>rotated</key><false/><key>sourceColorRect</key><string>{{0,0},{152,254}}</string><key>sourceSize</key><string>{152,254}</string></dict></dict><key>metadata</key><dict><key>format</key><integer>2</integer><key>realTextureFileName</key><string>dragon.png</string><key>size</key><string>{1024,2048}</string><key>smartupdate</key><string>$TexturePacker:SmartUpdate:26d1d28da42d49170ab3142654fea750:1/1$</string><key>textureFileName</key><string>dragon.png</string></dict></dict></plist>5.3 format为3的plist文件内容
12345678910111213141516171819202122232425262728293031323334353637383940414243<?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>frames</key><dict><key>armyLevelBg.png</key><dict><key>aliases</key><array/><key>spriteColorRect</key><string>{{0, 0}, {61, 36}}</string><key>spriteOffset</key><string>{0, -0}</string><key>spriteSize</key><string>{61, 36}</string><key>spriteSourceSize</key><string>{61, 36}</string><key>spriteTrimmed</key><true/><key>textureRect</key><string>{{195, 2}, {36, 61}}</string><key>textureRotated</key><true/></dict></dict><key>metadata</key><dict><key>version</key><string>1.6.0</string><key>format</key><integer>3</integer><key>size</key><string>{256, 512}</string><key>name</key><string>army_zw.zwd</string><key>textureFileName</key><string>army_zw_cocos2d.png</string><key>premultipliedAlpha</key><false/></dict></dict></plist>Frame:
Top-Left originating rectangle of the sprite’s pixel texture coordinates. Cocos2′d will convert these to UV coordinates (0-1) when loading based on the texture size.
Offset:
Zwoptex trim’s transparency off sprites. Because of this sprite’s need to be offset to ensure their texture is drawn in correct alignment to their original size.
Source Color Rect:
This is the Top-Left originating rectangle that is the valid pixel data of the sprite. Say you have a 512×512 sprite that only has 10×10 pixels of data inside of it located at 500×500. The source color rect could be {500,500,10,10}.
Format:
Version number related to what version of Zwoptex was used so cocos2d knows how to parse the plist properly.
Flash Version: 0
Desktop Version 0-0.4b: 1
Desktop Version 1.x: 2
In general if you’re not trimming images you can set offset to be 0,0 and sourceColorRect to 0,0,frame.size.width,frame.size.height.
http://www.cocos2d-iphone.org/forums/topic/zwoptex-and-their-plist-explanation/
5. Label纹理定义文件
对这种格式的具体的解析代码在CCLabelAtlas::initWithString 。
6. 帧动画定义文件格式说明
- 后面的篇幅会进行详细的介绍
您可能也喜欢: | ||||
![]() 关于cocos2d-x的一些记录 |
![]() TexturePacker工具的入门使用记录 |
![]() cocos2d-x2.2如何创建VS2010的win32新项目 |
![]() 闲聊博彩(2) |
![]() 宫崎骏插画 |
无觅 |
