Cocos2d-x   发布时间:2022-05-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了cocos2dx 资源 异步加载大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

本文转自:http://cn.cocos2d-x.org/tutorial/show?id=1277,感谢作者分享!

目前我代码中需要异步加载的资源一共有四类:

1.png,单个png的情况比较少

2.plist和png一起的打包资源

3.plist和pvr.ccz一起的打包资源

4.Cocosstudio导出的.ExportJson后缀的动画文件


再重复一遍加载机制:所有的资源是按序加载的,不可同时加载多个文件。执行异步加载的处理肯定是写在void update(float f)中,做到按帧加载。

1.png的异步加载

1
2
Director::geTinstance()->getTextureCache()->addImageAsync(reloadImages[curReloadImgNum],
CC_CALLBACK_1(LoadingScene::imageAsyncCallBACk, this ));

2.plist和png一起的打包资源

废话不多,直接上方法,没有加载plist的api,我们使用如1的方法加载png,然后在回调函数中加载plist。

2
Director::geTinstance()->getTextureCache()->addImageAsync(reloadPlists[curReloadPlistNum]+ "png" ,monospace!important; font-size:1em!important; min-height:inherit!important; BACkground:none!important">CC_CALLBACK_1(LoadingScene::plistImageAsyncCallBACk,serif; font-size:14px">

注意回调函数的参数,这是plist异步加载的关键,形参是png纹理。

2
3
4
5
6
void LoadingScene::plistImageAsyncCallBACk(cocos2d::Texture2D*texturE)
{
SpriteFrameCache::geTinstance()->addSpriteFramesWithFile(reloadPlists[curReloadPlistNum].append( "plist" ),texturE);
curReloadPlistNum++;
loading= true ;
}

3.plist和pvr.ccz一起的打包资源

类似2的方法。

6
7
8
Director::geTinstance()->getTextureCache()->addImageAsync(reloadPvrPlists[curReloadPvrPlistNum]+ "pvr.ccz" CC_CALLBACK_1(LoadingScene::pvrPlistImageAsyncCallBACk,monospace!important; font-size:1em!important; min-height:inherit!important; BACkground:none!important">));
LoadingScene::pvrPlistImageAsyncCallBACk(cocos2d::Texture2D*texturE)
{
SpriteFrameCache::geTinstance()->addSpriteFramesWithFile(reloadPvrPlists[curReloadPvrPlistNum].append( String" style="border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,texturE);
curReloadPvrPlistNum++;
;
4.Cocos studio导出的.ExportJson后缀的动画文件

7
ArmatureDataManager::geTinstance()->addArmatureFileInfoAsync(reloadExportJsons[curReloadExportJsonNum],monospace!important; font-size:1em!important; min-height:inherit!important; BACkground:none!important"> :1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,schedule_SELEctor(LoadingScene::JSonAsyncCallBACk));
LoadingScene::JSonAsyncCallBACk( float f)
curReloadExportJsonNum++;
;
四种加载方法已经介绍完了,只要在update中做好按序加载就可以了。

如何优化内存,加载尽量少的资源?

我的这个场景叫LoadingScene,这是一个中转场景,既是切换两个场景之间的过渡场景,用来释放上一个场景的资源和预加载下一个场景资源。

在这个场景中,我们可以释放掉cache中的所有文件,然后根据下个场景的内容来加载所需的文件。 这就需要我们做好配置工作,比如保卫萝卜,下一关只有两种塔,我们只加载这两种塔的动画即可,而不需要所有塔全部加载。

还有一个小技巧关于异步加载exportJson的文件。这个异步加载,既可以加载动画缓存,还可以把跟它一起的plist和png一起加载到缓存中,所有如果想加载plist,也可以把它们到成静止的动画,然后使用exportJson加载即可使用里面的精灵。

来源网址:http://www.cocoachina.com/bbs/read.php?tid=219154

大佬总结

以上是大佬教程为你收集整理的cocos2dx 资源 异步加载全部内容,希望文章能够帮你解决cocos2dx 资源 异步加载所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。