Cocos2d-x--使用CCSpriteFrameCache,CCAnimationCache预加载资源
来源:互联网 发布:mac safari清理缓存 编辑:程序博客网 时间:2024/06/06 12:34
Cocos2d-x--使用CCSpriteFrameCache,CCAnimationCache预加载资源
2013-7-4阅读398 评论0
用于预加载资源的缓冲区有:
CCTextureCache(图片纹理),CCSpriteFrameCache(精灵)
CCAnimationCache(动画),CCShaderCache(着色器)
请先参阅简单动画实现:http://blog.csdn.net/zlqqhs/article/details/9235551
所用到的图片资源:
步骤:
1.新建一个Cocos2d-x工程并保证能成功运行
2.在include下添加StaticData.h文件,添加代码
3.在source下添加StaticData.cpp文件,添加代码
4.在HelloWorldScene.cpp文件中添加和修改代码
1.新建一个Cocos2d-x工程,将所用到的图片资源放
到工程中,运行程序,保证程序能够成功运行
2.在include下添加StaticData.h文件,添加代码
#ifndef __STATICDATA_H__#define __STATICDATA_H__#include "cocos2d.h"class StaticData{public:static void load();};#endif
#include "StaticData.h"#include "cocos2d.h"using namespace cocos2d;void StaticData::load(){//1.读取2D纹理信息CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("items.png");//2.记录单帧信息CCSpriteFrame* m_frame1 = CCSpriteFrame::createWithTexture(texture, CCRectMake(/*0, 128, 32, 32*/64, 160, 64, 16));CCSpriteFrame* m_frame2 = CCSpriteFrame::createWithTexture(texture, CCRectMake(/*32, 128, 32, 32*/ 64, 176, 64, 16));CCSpriteFrame* m_frame3 = CCSpriteFrame::createWithTexture(texture, CCRectMake(/*64, 128, 32, 32*/64, 192, 64, 16));CCSpriteFrame* m_frame4 = CCSpriteFrame::createWithTexture(texture, CCRectMake(/*96, 128, 32, 32*/64, 208, 64, 16));//3.生成逐帧数组CCArray *animFrames = CCArray::create();animFrames->addObject(m_frame1);animFrames->addObject(m_frame2);animFrames->addObject(m_frame3);animFrames->addObject(m_frame4);//4.动画信息,设置间隔时间为0.5CCAnimation *animation = CCAnimation::createWithSpriteFrames(animFrames, 0.5f);//将CCAnimation加到CCAnimationCache缓冲区中CCAnimationCache::sharedAnimationCache()->addAnimation(animation, "animation");//将CCSpriteFrame加到CCAnimationCache缓冲区中CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFrame(m_frame1, "m_frame1");}
4.在HelloWorldScene.cpp文件中添加和修改代码
添加代码:
#include "StaticData.h"
删除bool HelloWorld::init()下do代码块中的代码,替换为:
CC_BREAK_IF(! CCLayer::init());StaticData::load();/***第一种方法***///先取得一个CCSpriteFrame,再通过取得的CCSpriteFrame创建一个CCSpriteCCSpriteFrame *m_frame1 = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("m_frame1");CCSprite *m_sprite = CCSprite::createWithSpriteFrame(m_frame1);m_sprite->setPosition(ccp(100, 100));this->addChild(m_sprite);/***第二种方法***///通过名字取得CCSpriteFrame//CCSprite *m_sprite = CCSprite::createWithSpriteFrameName("m_frame1");//m_sprite->setPosition(ccp(200, 200));//this->addChild(m_sprite);//通过名字取得CCAnimationCCAnimation *animation = CCAnimationCache::sharedAnimationCache()->animationByName("animation");CCAnimate *animate = CCAnimate::create(animation);m_sprite->runAction(CCRepeatForever::create(animate));bRet = true;
此为简单做法,通常情况下的做法是与plist配合使用,
与plist配合使用可提高维护性,简易化修改和增加
- Cocos2d-x--使用CCSpriteFrameCache,CCAnimationCache预加载资源
- Cocos2d-x--使用CCSpriteFrameCache,CCAnimationCache预加载资源
- cocos2d-x中的缓存CCTextureCache、CCSPriteFrameCache、CCAnimationCache
- cocos2d-x中的缓存CCTextureCache、CCSPriteFrameCache、CCAnimationCache
- Cocos2d-x中的 CCSpriteFrameCache , CCTextureCache的使用
- cocos2d-x CCSpriteFrameCache类使用总结
- Cocos2d-x之CCSpriteFrameCache
- cocos2d-x CCSpriteFrameCache
- cocos2d-x -CCSpriteFrameCache-CCAnimation
- cocos2d-x 动画缓存CCAnimationCache
- 缓存与预加载技术(图片缓存CCSpriteFrameCache、动画缓存 CCAnimationCache、纹理缓存CCTextureCache、音乐音效SimpleAudioEngine)
- cocos2d-x 使用精灵帧缓存池CCSpriteFrameCache
- cocos2d-x 3.0 预加载pvr资源
- cocos2d-X 节点(CCAnimationCache.h)API
- 预加载+CCSpriteBatchNode + CCSpriteFrameCache
- Cocos2d-x 资源加载进度条
- cocos2d-x之 CCSpriteFrameCache 用法总结
- cocos2d-x 动画详解之四-CCSpriteFrameCache
- 第八周项目1-分段函数求值(2013级新)
- Android应用:SoundPool 播放音效
- 指针的复杂声明
- HOG相关研究资料
- Java:集合与数组转换
- Cocos2d-x--使用CCSpriteFrameCache,CCAnimationCache预加载资源
- C++的XML编程经验――LIBXML2库使用指南
- Android中include和merge标签的使用(附源码)
- system的默认密码是 manager sys的默认密码是 changer_on_install
- struts2+hibernate+spring 分页
- FLEX相关
- 第8周项目1-求简分段函数的值
- linux skb_buff
- 第7周项目2-输入三个整数求最大值