![cocos2dx 资源 异步加载,第1张 cocos2dx 资源 异步加载,第1张](/aiimages/cocos2dx+%E8%B5%84%E6%BA%90+%E5%BC%82%E6%AD%A5%E5%8A%A0%E8%BD%BD.png)
概述本文转自:http://cn.cocos2d-x.org/tutorial/show?id=1277,感谢作者分享! 目前我代码中需要
异步加载的
资源一共有四类: 1.png,单个png的情况比较少 2.plist和png一起的打包资源 3.plist和pvr.ccz一起的打包资源 4.CocosStudio导出的.ExportJson后缀的动画文件 再重复一遍
加载机制:所有的资源是按序加载的,不可
本文转自: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( curReloadPvrPListNum++; ; 4.Cocos Studio导出的.ExportJson后缀的动画文件 7 ArmatureDataManager::getInstance()->addArmaturefileInfoAsync(reloadExportJsons[curReloadExportJsonNum],monospace!important; Font-size:1em!important; min-height:inherit!important; background:none!important"> LoadingScene::JsonAsyncCallback( float f) curReloadExportJsonNum++; ; 四种加载方法已经介绍完了,只要在update中做好按序加载就可以了。 如何优化内存,加载尽量少的资源? 我的这个场景叫LoadingScene,这是一个中转场景,既是切换两个场景之间的过渡场景,用来释放上一个场景的资源和预加载下一个场景资源。 在这个场景中,我们可以释放掉cache中的所有文件,然后根据下个场景的内容来加载所需的文件。 这就需要我们做好配置工作,比如保卫萝卜,下一关只有两种塔,我们只加载这两种塔的动画即可,而不需要所有塔全部加载。 还有一个小技巧关于异步加载exportJson的文件。这个异步加载,既可以加载动画缓存,还可以把跟它一起的pList和png一起加载到缓存中,所有如果想加载pList,也可以把它们到成静止的动画,然后使用exportJson加载即可使用里面的精灵。 来源网址:http://www.cocoachina.com/bbs/read.php?tid=219154 总结 以上是内存溢出为你收集整理的cocos2dx 资源 异步加载全部内容,希望文章能够帮你解决cocos2dx 资源 异步加载所遇到的程序开发问题。 如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
赞
(0)
打赏
微信扫一扫
支付宝扫一扫
| | |
评论列表(0条)