cocos2d-x 2.1 -- 读取.plist文件
在cocos2d-x中可以用.plist格式的文件来保存数据,它是XML文件格式的一种,在cocos2d-x解析.plist方面相关的资料比较少,但本身也很简单,要解析.plist文件可以参考cocos2d-x类库中的CCSpriteFrameCache类和CCParticleSystem类,它主要是使用CCDictionary类来对.plist文件进行操作。
下面有一个.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>level1</key>
- <dict>
- <key>bg_far_scene</key>
- <dict>
- <key>path</key>
- <string>images/far_scene.png</string>
- <key>pos</key>
- <string>{358, 309}</string>
- </dict>
- <key>bg_near_scene</key>
- <dict>
- <key>path</key>
- <string>images/near_scene.png</string>
- <key>pos</key>
- <string>{360, 100}</string>
- </dict>
- </dict>
- </dict>
- </plist>
读取.plist文件的代码如下:
- const char* testPlistPath = "BSPlistDatas\\test.plist";
- const char* fullPath = CCFileUtils::sharedFileUtils()->fullPathFromRelativeFile("test.plist", testPlistPath);
- CCDictionary* plistDic = CCDictionary::createWithContentsOfFile(testPlistPath);
- CCDictionary* levelDic = dynamic_cast<CCDictionary*>(plistDic->objectForKey("level1"));
- CCDictionary* farScene = dynamic_cast<CCDictionary*>(levelDic->objectForKey("bg_far_scene"));
- CCString* farScenePath = dynamic_cast<CCString*>(farScene->objectForKey("path"));
- CCPoint point = CCPointFromString(farScene->valueForKey("pos")->getCString());
- CCLog("path = %s", farScenePath->getCString());
- CCLog("pos = %f, %f", point.x, point.y);
第一行是.plist文件的相对路径,通过CCFileUtils类获得文件中绝对路径后,使用CCDictionary::createWithContensOfFile(filePath);将文件中内容加载到CCDictionary数据结构的内存中,然后通过xxxForKey获得相应的key下的value。
这里需要注意的是,当在读取'pos'的时候,它的值一个{x, y}的字符串,这是.plist文件中的数组存储规则,我们可以通过cocos2d-x提供函数api将这样的字符串转化为CCpoint对象。
- <span style="white-space:pre"> </span>CCPoint point = CCPointFromString(farScene->valueForKey("pos")->getCString());
上面这句话就是做了这样的一个转化的过程,同样的cocos2d-x还支持CCSize、CCRect的字符串的转化。他们转化的方法以及在.plist中对应的字符串格式如下:
CCPoint: CCPointFromString();{x, y}
CCSize: CCSizeFromString();{w, h}
CCRect: CCSizeFromString();{x, y, w, h}
这样我们2D游戏所初始化所需要的数据都基本上够用了,可以尝试将游戏的初始数据放在.plist中,然后修改调整数值就可以直接修改plist文件,而无需重新编译程序了,从而实现游戏数据和游戏逻辑的分离。
在cocos2d-x中可以用.plist格式的文件来保存数据,它是XML文件格式的一种,在cocos2d-x解析.plist方面相关的资料比较少,但本身也很简单,要解析.plist文件可以参考cocos2d-x类库中的CCSpriteFrameCache类和CCParticleSystem类,它主要是使用CCDictionary类来对.plist文件进行操作。
下面有一个.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>level1</key>
- <dict>
- <key>bg_far_scene</key>
- <dict>
- <key>path</key>
- <string>images/far_scene.png</string>
- <key>pos</key>
- <string>{358, 309}</string>
- </dict>
- <key>bg_near_scene</key>
- <dict>
- <key>path</key>
- <string>images/near_scene.png</string>
- <key>pos</key>
- <string>{360, 100}</string>
- </dict>
- </dict>
- </dict>
- </plist>
读取.plist文件的代码如下:
- const char* testPlistPath = "BSPlistDatas\\test.plist";
- const char* fullPath = CCFileUtils::sharedFileUtils()->fullPathFromRelativeFile("test.plist", testPlistPath);
- CCDictionary* plistDic = CCDictionary::createWithContentsOfFile(testPlistPath);
- CCDictionary* levelDic = dynamic_cast<CCDictionary*>(plistDic->objectForKey("level1"));
- CCDictionary* farScene = dynamic_cast<CCDictionary*>(levelDic->objectForKey("bg_far_scene"));
- CCString* farScenePath = dynamic_cast<CCString*>(farScene->objectForKey("path"));
- CCPoint point = CCPointFromString(farScene->valueForKey("pos")->getCString());
- CCLog("path = %s", farScenePath->getCString());
- CCLog("pos = %f, %f", point.x, point.y);
第一行是.plist文件的相对路径,通过CCFileUtils类获得文件中绝对路径后,使用CCDictionary::createWithContensOfFile(filePath);将文件中内容加载到CCDictionary数据结构的内存中,然后通过xxxForKey获得相应的key下的value。
这里需要注意的是,当在读取'pos'的时候,它的值一个{x, y}的字符串,这是.plist文件中的数组存储规则,我们可以通过cocos2d-x提供函数api将这样的字符串转化为CCpoint对象。
- <span style="white-space:pre"> </span>CCPoint point = CCPointFromString(farScene->valueForKey("pos")->getCString());
上面这句话就是做了这样的一个转化的过程,同样的cocos2d-x还支持CCSize、CCRect的字符串的转化。他们转化的方法以及在.plist中对应的字符串格式如下:
CCPoint: CCPointFromString();{x, y}
CCSize: CCSizeFromString();{w, h}
CCRect: CCSizeFromString();{x, y, w, h}
这样我们2D游戏所初始化所需要的数据都基本上够用了,可以尝试将游戏的初始数据放在.plist中,然后修改调整数值就可以直接修改plist文件,而无需重新编译程序了,从而实现游戏数据和游戏逻辑的分离。
cocos2d-x 2.1 -- 读取.plist文件的更多相关文章
- cocos2d-x 读取.plist文件
转自:http://blog.csdn.net/hgplan/article/details/8629904 在cocos2d-x中可以用.plist格式的文件来保存数据,它是XML文件格式的一种,在 ...
- 黑马程序员——读取Plist文件
-iOS培训,iOS学习-------型技术博客.期待与您交流!------------ 读取Plist文件 一:新建一个plist文件,并将plist文件数据填入plist文件中,这里pli ...
- cocos2d-实现读取.plist文件(使用数组CCArray)
学习札记之cocos2d-x2.1.1实现读取.plist文件(使用数组CCArray) <?xml version="1.0" encoding="UTF-8&q ...
- iOS开发读取plist文件、iphone中plist文件的
在Xcode中建立一个iOS项目后,会自己产生一个.plist文件,点击时会看见它显示的是类似于excel表格: 但是,如果打开方式选择Source Code,你会看见它其实是一个xml文件. 我们会 ...
- 解决pathForResource返回nil, 无法读取plist文件问题
有很多人在设置plist文件的时候, 会发现读取不了plist文件里面的内容, 返回值为nil, 下面我们来解决一下这个问题. 首先我们打开工程并且按照下面的步骤来设置: 设置好后, 我们来写一段代码 ...
- [How to]如何自定义plist文件和读取plist文件内容
1.简介 plist作为IOS的固化文件,就好比java中properties文件,但是在IOS中plist是可读写的. 本文将介绍自定义静态的plist文件. 2.自定义静态plist文件 右击你的 ...
- iOS-如何读取Plist文件
解决办法: // 1) 找到Plist文件的路径 "path" NSString *path = [[NSBundle mainBundle]pathForResource:@&q ...
- ios读取plist文件:
@property (nonatomic,strong) NSArray *imageData;//定义一个数组 //懒加载数据 -(NSArray *)imageDate { if(_imageDa ...
- plist文件的读取和xib加载cell
plist 文件读取 例如在工程里倒入了plist文件 在工程里需要用到plist文件里的信息,就需要把plist文件读取出来. 如程序: -(NSArray *)moreDataArr{ if (! ...
随机推荐
- 警告:Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
加入 import os os.environ[' demo: import os os.environ[' import tensorflow as tf tf.enable_eager_execu ...
- Qt5.3.2_CentOS6.4(x86)_代码文件编码
1.1.1.Qt5.3.2_MinGW 在Windows中安装时,默认的文件编码是 UTF8. 1.2.在 CentOS6.4中安装 qt-opensource-linux-x86-5.3.2.run ...
- 《剑指offer》第十九题(正则表达式匹配)
// 面试题19:正则表达式匹配 // 题目:请实现一个函数用来匹配包含'.'和'*'的正则表达式.模式中的字符'.' // 表示任意一个字符,而'*'表示它前面的字符可以出现任意次(含0次).在本题 ...
- sublime text注册码与快捷键
其他版本: —– BEGIN LICENSE —– Michael Barnes Single User License EA7E-821385 8A353C41 872A0D5C DF9B2950 ...
- js插件---在线类似excel生成图表插件解决方案
js插件---在线类似excel生成图表插件解决方案 一.总结 一句话总结:google比百度好用多了,多用google google js editable table jquery 双向绑定 这种 ...
- freemarker中对null值问题的处理
1. freemarker不支持null. 如果值为null会报错. 2.当值为null的处理 1)过滤不显示 Hello ${name!} 在属性后面加感叹号即可过滤null和空字符串 if和”?? ...
- 笔试题-sql语句
今天遇到了不熟练(不会)的查询题目 回来自己又做了一下,如下 建表语句 -- Table structure for score -- ---------------------------- DRO ...
- LeetCode--219--存在重复元素2
问题描述: 给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 的差的绝对值最大为 k. 示例 1: 输入: ...
- 12月17日周日 form_for的部分理解。belongs_to的部分理解
1.lean guide:helper method query ,✅
- Confluence 6 为站点启用匿名用户访问
如果你希望你的站点能够被所有人看到,包括不需要登录就可以访问的用户.你必须为你的站点启用匿名用户访问权限才可以. 希望启用匿名用户访问你的站点: 在屏幕的右上角单击 控制台按钮 ,然后选择 Gener ...