最近在做一项工作,将基于cocos2d-iphone游戏转换为跨平台版本。
以下为OC代码:
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"game_ui.plist"]; [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"game_effect.plist"];
以下为Lua代码(我们的UI采用Lua编码):
display.addSpriteFramesWithFile('game_ui.plist') display.addSpriteFramesWithFile('game_effect.plist')
运行时,却发现了在OC版本中没有出现的异常“CCSprite is not using the same texture id”
跟踪该png文件,却发现health_bar.png文件同时存在于这两个plist。
这时候就产生疑问了,OC和C++版本的资源文件都是一样的。为什么cocos2d-x就会报异常呢?
好在cocos2d是开源的,所以开始研究addSpriteFramesWithFile的实现细节。
cocos2d-iphone
-(void) addSpriteFramesWithFile:(NSString*)plist { NSAssert(plist, @"plist filename should not be nil"); if( ! [_loadedFilenames member:plist] ) { NSString *path = [[CCFileUtils sharedFileUtils] fullPathForFilename:plist]; NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path]; NSString *texturePath = nil; NSDictionary *metadataDict = [dict objectForKey:@"metadata"]; if( metadataDict ) // try to read texture file name from meta data texturePath = [metadataDict objectForKey:@"textureFileName"]; if( texturePath ) { // build texture path relative to plist file NSString *textureBase = [plist stringByDeletingLastPathComponent]; texturePath = [textureBase stringByAppendingPathComponent:texturePath]; } else { // build texture path by replacing file extension texturePath = [plist stringByDeletingPathExtension]; texturePath = [texturePath stringByAppendingPathExtension:@"png"]; CCLOG(@"cocos2d: CCSpriteFrameCache: Trying to use file '%@' as texture", texturePath); } [self addSpriteFramesWithDictionary:dict textureFilename:texturePath]; [_loadedFilenames addObject:plist]; } else CCLOGINFO(@"cocos2d: CCSpriteFrameCache: file already loaded: %@", plist); }
cocos2d-x
void CCSpriteFrameCache::addSpriteFramesWithDictionary(CCDictionary* dictionary, CCTexture2D *pobTexture) { /* Supported Zwoptex Formats: ZWTCoordinatesFormatOptionXMLLegacy = 0, // Flash Version ZWTCoordinatesFormatOptionXML1_0 = 1, // Desktop Version 0.0 - 0.4b ZWTCoordinatesFormatOptionXML1_1 = 2, // Desktop Version 1.0.0 - 1.0.1 ZWTCoordinatesFormatOptionXML1_2 = 3, // Desktop Version 1.0.2+ */ CCDictionary *metadataDict = (CCDictionary*)dictionary->objectForKey("metadata"); CCDictionary *framesDict = (CCDictionary*)dictionary->objectForKey("frames"); int format = 0; // get the format if(metadataDict != NULL) { format = metadataDict->valueForKey("format")->intValue(); } // check the format CCAssert(format >=0 && format <= 3, "format is not supported for CCSpriteFrameCache addSpriteFramesWithDictionary:textureFilename:"); CCDictElement* pElement = NULL; CCDICT_FOREACH(framesDict, pElement) { CCDictionary* frameDict = (CCDictionary*)pElement->getObject(); std::string spriteFrameName = pElement->getStrKey(); CCSpriteFrame* spriteFrame = (CCSpriteFrame*)m_pSpriteFrames->objectForKey(spriteFrameName); if (spriteFrame) { continue; } if(format == 0) { float x = frameDict->valueForKey("x")->floatValue(); float y = frameDict->valueForKey("y")->floatValue(); float w = frameDict->valueForKey("width")->floatValue(); float h = frameDict->valueForKey("height")->floatValue(); float ox = frameDict->valueForKey("offsetX")->floatValue(); float oy = frameDict->valueForKey("offsetY")->floatValue(); int ow = frameDict->valueForKey("originalWidth")->intValue(); int oh = frameDict->valueForKey("originalHeight")->intValue(); // check ow/oh if(!ow || !oh) { CCLOGWARN("cocos2d: WARNING: originalWidth/Height not found on the CCSpriteFrame. AnchorPoint won't work as expected. Regenrate the .plist"); } // abs ow/oh ow = abs(ow); oh = abs(oh); // create frame spriteFrame = new CCSpriteFrame(); spriteFrame->initWithTexture(pobTexture, CCRectMake(x, y, w, h), false, CCPointMake(ox, oy), CCSizeMake((float)ow, (float)oh) ); } else if(format == 1 || format == 2) { CCRect frame = CCRectFromString(frameDict->valueForKey("frame")->getCString()); bool rotated = false; // rotation if (format == 2) { rotated = frameDict->valueForKey("rotated")->boolValue(); } CCPoint offset = CCPointFromString(frameDict->valueForKey("offset")->getCString()); CCSize sourceSize = CCSizeFromString(frameDict->valueForKey("sourceSize")->getCString()); // create frame spriteFrame = new CCSpriteFrame(); spriteFrame->initWithTexture(pobTexture, frame, rotated, offset, sourceSize ); } else if (format == 3) { // get values CCSize spriteSize = CCSizeFromString(frameDict->valueForKey("spriteSize")->getCString()); CCPoint spriteOffset = CCPointFromString(frameDict->valueForKey("spriteOffset")->getCString()); CCSize spriteSourceSize = CCSizeFromString(frameDict->valueForKey("spriteSourceSize")->getCString()); CCRect textureRect = CCRectFromString(frameDict->valueForKey("textureRect")->getCString()); bool textureRotated = frameDict->valueForKey("textureRotated")->boolValue(); // get aliases CCArray* aliases = (CCArray*) (frameDict->objectForKey("aliases")); CCString * frameKey = new CCString(spriteFrameName); CCObject* pObj = NULL; CCARRAY_FOREACH(aliases, pObj) { std::string oneAlias = ((CCString*)pObj)->getCString(); if (m_pSpriteFramesAliases->objectForKey(oneAlias.c_str())) { CCLOGWARN("cocos2d: WARNING: an alias with name %s already exists", oneAlias.c_str()); } m_pSpriteFramesAliases->setObject(frameKey, oneAlias.c_str()); } frameKey->release(); // create frame spriteFrame = new CCSpriteFrame(); spriteFrame->initWithTexture(pobTexture, CCRectMake(textureRect.origin.x, textureRect.origin.y, spriteSize.width, spriteSize.height), textureRotated, spriteOffset, spriteSourceSize); } // add sprite frame m_pSpriteFrames->setObject(spriteFrame, spriteFrameName); spriteFrame->release(); } } void CCSpriteFrameCache::addSpriteFramesWithFile(const char *pszPlist, CCTexture2D *pobTexture) { std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszPlist); CCDictionary *dict = CCDictionary::createWithContentsOfFileThreadSafe(fullPath.c_str()); addSpriteFramesWithDictionary(dict, pobTexture); dict->release(); }
OK!事情明了了。cocos2d-x比cocos2d-iphone多做了一步校验。
CCSpriteFrame* spriteFrame = (CCSpriteFrame*)m_pSpriteFrames->objectForKey(spriteFrameName); if (spriteFrame) { continue; }
然后,我将c++版本的plist加载顺序调整一下。搞定。
多说一句,一个png就不应该同时添加到两个plist中。
相关推荐
使用`CCSpriteFrameCache`的`addSpriteFramesWithFile:`方法加载`.plist`文件。 ```swift let frameCache = CCSpriteFrameCache.shared() frameCache.addSpriteFramesWithFile("background.plist") ``` 2. 创建一个`...
尽管如此,重复加载同一部分的图片不会再增加额外的内存,因为它们已经存在于内存中。 为了控制内存占用,开发者需要适时地释放缓存。在场景切换时,可以清理前一场景的资源。Cocos2d-x提供了如CCTextureCache的...
2. 获取精灵帧:通过` CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile()` 添加精灵帧信息,通常是.plist文件,它包含了精灵表的布局信息。 3. 创建动画:使用` CCArray `存储` ...
Java项目基于springboot的课程设计,包含源码+数据库+毕业论文
Java 项目, Java 毕业设计,Java 课程设计,基于 SpringBoot 开发的,含有代码注释,新手也可看懂。毕业设计、期末大作业、课程设计、高分必看,下载下来,简单部署,就可以使用。 包含:项目源码、数据库脚本、软件工具等,前后端代码都在里面。 该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。 项目都经过严格调试,确保可以运行! 1. 技术组成 前端:html、javascript、Vue 后台框架:SpringBoot 开发环境:idea 数据库:MySql(建议用 5.7 版本,8.0 有时候会有坑) 数据库工具:navicat 部署环境:Tomcat(建议用 7.x 或者 8.x 版本), maven 2. 部署 如果部署有疑问的话,可以找我咨询 Java工具包下载地址: https://pan.quark.cn/s/eb24351ebac4 后台路径地址:localhost:8080/项目名称/admin/dist/index.html 前台路径地址:localhost:8080/项目名称/front/index.html (无前台不需要输入)
GUI面板MATLAB漂浮物识别
【工程项目】MATLAB道路桥梁裂缝检测[不同类型,GUI界面,Bp算法]
Delphi 12.3控件之高仿银豹手机APP 1.0.zip
ava项目springboot基于springboot的课程设计,包含源码+数据库+毕业论文
Java 项目, Java 毕业设计,Java 课程设计,基于 SpringBoot 开发的,含有代码注释,新手也可看懂。毕业设计、期末大作业、课程设计、高分必看,下载下来,简单部署,就可以使用。 包含:项目源码、数据库脚本、软件工具等,前后端代码都在里面。 该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。 项目都经过严格调试,确保可以运行! 1. 技术组成 前端:jsp 后台框架:SSM 开发环境:idea 数据库:MySql(建议用 5.7 版本,8.0 有时候会有坑) 数据库工具:navicat 部署环境:Tomcat(建议用 7.x 或者 8.x 版本), maven 2. 部署 如果部署有疑问的话,可以找我咨询 Java工具包下载地址: https://pan.quark.cn/s/eb24351ebac4
GUI面板MATLAB指纹识别
【工程项目】MATLAB的病虫害检测系统(SVM方法,GUI界面)
Java 项目, Java 毕业设计,Java 课程设计,基于 SSM 开发的,含有代码注释,新手也可看懂。毕业设计、期末大作业、课程设计、高分必看,下载下来,简单部署,就可以使用。 包含:项目源码、数据库脚本、软件工具等,前后端代码都在里面。 该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。 项目都经过严格调试,确保可以运行! 1. 技术组成 前端:vue/html5 后台框架:SSM 开发环境:idea 数据库:MySql(建议用 5.7 版本,8.0 有时候会有坑) 数据库工具:navicat 部署环境:Tomcat(建议用 7.x 或者 8.x 版本), maven 2. 部署 如果部署有疑问的话,可以找我咨询 Java工具包下载地址: https://pan.quark.cn/s/eb24351ebac4
Java 项目, Java 毕业设计,Java 课程设计,基于 SpringBoot 开发的,含有代码注释,新手也可看懂。毕业设计、期末大作业、课程设计、高分必看,下载下来,简单部署,就可以使用。 包含:项目源码、数据库脚本、软件工具等,前后端代码都在里面。 该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。 项目都经过严格调试,确保可以运行! 1. 技术组成 前端:html、javascript、Vue 后台框架:SpringBoot 开发环境:idea 数据库:MySql(建议用 5.7 版本,8.0 有时候会有坑) 数据库工具:navicat 部署环境:Tomcat(建议用 7.x 或者 8.x 版本), maven 2. 部署 如果部署有疑问的话,可以找我咨询 Java工具包下载地址: https://pan.quark.cn/s/eb24351ebac4 后台路径地址:localhost:8080/项目名称/admin/dist/index.html 前台路径地址:localhost:8080/项目名称/front/index.html (无前台不需要输入)
Java 项目, Java 毕业设计,Java 课程设计,基于 SpringBoot 开发的,含有代码注释,新手也可看懂。毕业设计、期末大作业、课程设计、高分必看,下载下来,简单部署,就可以使用。 包含:项目源码、数据库脚本、软件工具等,前后端代码都在里面。 该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。 项目都经过严格调试,确保可以运行! 1. 技术组成 前端:html、javascript、Vue 后台框架:SpringBoot 开发环境:idea 数据库:MySql(建议用 5.7 版本,8.0 有时候会有坑) 数据库工具:navicat 部署环境:Tomcat(建议用 7.x 或者 8.x 版本), maven 2. 部署 如果部署有疑问的话,可以找我咨询 Java工具包下载地址: https://pan.quark.cn/s/eb24351ebac4 后台路径地址:localhost:8080/项目名称/admin/dist/index.html 前台路径地址:localhost:8080/项目名称/front/index.html (无前台不需要输入)
2023年计算机组成与维护教程题库及答案.pdf
本文章是本人在学习初阶数据结构的随手笔记,不具有任何教学功能,仅供参考
#深度学习环境配置#CUDA+CUDNN+pycharm
ava项目springboot基于springboot的课程设计,包含源码+数据库+毕业论文
2023年全国计算机二级笔记.pdf