`
song020cn
  • 浏览: 64081 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

CCSpriteFrameCache添加plist时不同plist中存在重复的文件

阅读更多

 

最近在做一项工作,将基于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中。

 

分享到:
评论

相关推荐

    cocos2d实现无限重复的游戏背景动画

    使用`CCSpriteFrameCache`的`addSpriteFramesWithFile:`方法加载`.plist`文件。 ```swift let frameCache = CCSpriteFrameCache.shared() frameCache.addSpriteFramesWithFile("background.plist") ``` 2. 创建一个`...

    剖析iOS开发中Cocos2d-x的内存管理相关操作

    尽管如此,重复加载同一部分的图片不会再增加额外的内存,因为它们已经存在于内存中。 为了控制内存占用,开发者需要适时地释放缓存。在场景切换时,可以清理前一场景的资源。Cocos2d-x提供了如CCTextureCache的...

    Sprite_Sheet_Animation:在cocos2dx for android平台使用精灵表创建动画

    2. 获取精灵帧:通过` CCSpriteFrameCache::sharedSpriteFrameCache()-&gt;addSpriteFramesWithFile()` 添加精灵帧信息,通常是.plist文件,它包含了精灵表的布局信息。 3. 创建动画:使用` CCArray `存储` ...

    制定CA6140车床拨叉的加工工艺,设计钻φ5孔的钻床夹具设计.rar

    制定CA6140车床拨叉的加工工艺,设计钻φ5孔的钻床夹具设计.rar

    128 基于STM32的儿童误锁车内远程报警系统【QT上位机源码】.zip

    这是 《128 基于STM32的儿童误锁车内远程报警系统【QT上位机源码】》 项目的Qt上位机上位机源码包。 这是一个Qt工程,采用QT5.12.6版本开发的源码。支持生成Windows系统运行程序。也支持生成Android手机APP。 对应项目的博客链接:https://blog.csdn.net/xiaolong1126626497/article/details/132015856 注意 注意 注意!!!: 如果不需要修改上位机源码,就不用下载本资源 (本项目的STM32源码包里就包含了上位机APP安装包,可以直接使用),在设计文档里也写了上位机的核心代码。 如果想学习本项目的上位机开发,学习上位机的源码,修改源。那么可以下载。 最好自己具备一定的Qt开发基础。

    水泥粉磨生产工艺流程图.zip

    水泥粉磨生产工艺流程图.zip

    ParagonHFS+forWin v14.0.24 x64.rar

    WINDOWS系统读取苹果分区的利器,支持HFS+及APFS分区。

    基于Ryu 控制器和 Mininet 实现软件定义网络(SDN)负载均衡解决方案,用于网络模拟.zip

    基于Ryu 控制器和 Mininet 实现软件定义网络(SDN)负载均衡解决方案,用于网络模拟.zip

    20250415API翻譯

    20250415API翻譯

    Git知识学习(尚硅谷)

    Git知识学习(尚硅谷)

    手机充电器的模具设计.zip

    手机充电器的模具设计.zip

    原神弹琴脚本,让风告诉你,只需要pynput库即可使用

    python

    基于SpringBoot的体育商品推荐系统(源码+数据库+万字文档+ppt)535

    基于SpringBoot的体育商品推荐系统,系统包含两种角色:管理员、用户主要功能如下。 【用户功能】 1. **首页:** 浏览体育商品推荐系统的主要信息。 2. **商品信息:** 查看系统推荐的体育商品。 3. **交流论坛:** 参与用户间的体育商品讨论和交流。 4. **公告资讯:** 查看系统发布的重要通知和体育商品资讯。 5. **留言板:** 发表个人意见和留言,参与系统互动。 6. **购物车:** 查看已选购的体育商品,进行结算和下单。 7. **个人中心:** 管理个人信息,查看订单历史和进行相关操作。 【管理员功能】 1. **首页:** 查看体育商品推荐系统。 2. **个人中心:** 修改密码、管理个人信息。 3. **用户管理:** 审核和管理注册用户的信息。 4. **商品分类管理:** 管理体育商品的分类信息。 5. **商品信息管理:** 监管和管理体育商品的信息。 6. **交流论坛:** 管理用户间的讨论和交流,包括删除不当内容。 7. **留言板:** 管理用户的留言,进行适当的处理。 8. **系统管理:** - **轮播图管理:** 管理系统首页的轮播图,包括添加、编辑和删除。 - **关于我们:** 编辑和更新关于体育商品推荐系统的介绍。 - **公告资讯:** 发布、编辑和删除系统的通知和公告。 - **系统简介:** 提供体育商品推荐系统的简要介绍。 9. **订单管理:** - **已退款订单:** 查看和管理已退款的订单信息。 - **未支付订单:** 查看和管理未支付的订单信息。 - **已发货订单:** 查看和管理已发货但未完成的订单信息。 - **已支付订单:** 查看和管理已支付但未完成的订单信息。 - **已完成订单:** 查看和管理已完成的订单信

    ### 【物联网操作系统】LiteOS从入门到实战:开发环境搭建、内核解析及网络编程详解、LiteOS简介

    内容概要:本文详细介绍了LiteOS这一轻量级物联网操作系统,涵盖其特点、应用场景、开发环境搭建、内核机制、实战演练及进阶学习。LiteOS由华为开发,专为资源受限设备设计,具备轻量级、高效性、安全性和开放性等特点,适用于智能家居、工业自动化、智能穿戴和智能城市建设等领域。文章逐步讲解了Windows和Linux系统下搭建LiteOS开发环境的具体步骤,包括安装交叉编译器、HiSpark Studio、配置Python环境、下载并配置LiteOS SDK等。深入探讨了LiteOS内核的任务管理和内存管理机制,并通过Hello World程序展示了创建任务、编写代码、编译和烧录的完整流程。最后,介绍了SAL及socket编程,提供了丰富的学习资源,包括官方文档、技术论坛和开源代码库。 适合人群:具备一定编程基础,尤其是对物联网开发感兴趣的开发者,以及希望深入了解嵌入式操作系统原理的技术人员。 使用场景及目标:①学习如何在资源受限的设备上开发高效稳定的应用程序;②掌握LiteOS的任务管理、内存管理等核心机制;③通过实战演练和进阶学习,提高物联网设备的网络通信能力,如使用SAL及socket编程实现设备与服务器之间的TCP通信。 其他说明:本文不仅提供了理论知识,还结合具体代码示例和实际操作步骤,帮助读者更好地理解和应用LiteOS。物联网技术正处于快速发展阶段,掌握LiteOS开发技能将为开发者在智能家居、工业自动化、智能穿戴等领域提供强大的竞争力。

    Android14请求存储权限适配demo源码

    Android开发14版本请求存储权限,它有部分允许权限,有一点难度。

    在vs集成开发环境中,使用C/C++开发的游戏:球球大作战(注意要使用EasyX库)

    在vs集成开发环境中,使用C/C++开发的游戏:球球大作战(注意要使用EasyX库)

    【露天矿山边坡安全监测】基于技术规范的金属非金属露天矿山边坡安全监测系统设计与实施:变形、应力、爆破振动及水文气象监测方法和要求

    内容概要:本文档为《露天矿山边坡安全监测技术规范》,旨在规定金属非金属露天矿山采场边坡安全监测的原则、内容、方法和技术要求,涵盖变形监测、采动应力监测、爆破振动监测、降雨和地下水监测、视频监控、在线监测系统等方面。文档详细介绍了监测系统的安装、维护和监测资料的整理分析等管理要求。通过定义边坡分类、安全监测分级、监测要求和具体监测方法,确保露天矿山边坡的安全性和稳定性。 适用人群:适用于从事露天矿山边坡安全监测的设计、施工、管理和研究人员,以及矿山企业的安全管理人员。 使用场景及目标:①用于指导露天矿山边坡的安全监测工作,确保监测系统的设计、安装、调试和运行符合标准;②通过对边坡变形、应力、爆破振动、水文气象等进行监测,预防和控制边坡失稳事故的发生;③利用在线监测系统和数据分析,实现对边坡安全状况的实时监控和预警。 其他说明:本文档不适用于与煤共生、伴生的金属非金属露天矿山采场边坡。文档提供了详细的监测方法和要求,强调了监测系统的兼容性、可扩展性和数据的安全存储。此外,还特别强调了定期巡查和信息反馈机制的重要性,以确保监测系统的有效运行和及时响应异常情况。

    acacia_door_bottom.png

    acacia_door_bottom

    Android开发不用存储权限进行拍照demo源码

    Android开发不用存储权限进行拍照,得到拍照后的图片效果。有一点难度,关键是存储路径的定义。

    27910240_g.zip

    27910240_g.zip

Global site tag (gtag.js) - Google Analytics