标签:
Cocos中有三种缓存类:
(1):纹理缓存:TextureCache
(2):精灵帧缓存:SpriteFrameCache
(3):动画缓存:AnimationCache
游戏最要重要的就是流畅度,如果我们的游戏经常因为加载资源出现卡顿的情况,那么这个游戏就没有很好地游戏体验。所以,为了解决这个问题,缓存就应运而生了。
缓存的目的就是:现将所需资源加载到内存中,之后再次使用该资源的时候,就可以直接从内存中读出,而不需要重新加载,从而减少了CPU和GPU的内存占用。
//
Sprite* Sprite::create(const std::string& filename)
{
Sprite *sprite = new Sprite();
if (sprite && sprite->initWithFile(filename))
{
sprite->autorelease();
return sprite;
}
_SAFE_DELETE(sprite);
return nullptr;
}
bool Sprite::initWithFile(const std::string& filename)
{
ASSERT(filename.size()>0, "Invalid filename for sprite");
// 加载filename的纹理图片
Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(filename);
if (texture)
{
Rect rect = Rect::ZERO;
rect.size = texture->getContentSize();
return initWithTexture(texture, rect);
}
return false;
}
//可以看到这个精灵被加载到纹理缓存中了。//第一种载入精灵的方法
auto sp1 = Sprite::create("boy.png");
this->addChild(sp1,1);
//第二种载入精灵的方法
auto sp_cache = Director::getInstance()->addImage("boy.png");
auto sp1 = Sprite::createWithTexture(sp_cache);
this->addChild(sp1,1);// // 获取单例对象 SpriteFrameCache* cache = SpriteFrameCache::getInstance(); // 销毁单例对象 SpriteFrameCache::destroyInstance(); //
//
// boy.png 里集合了boy1.png,boy2.png这些小图。
// 参数2 可不写
SpriteFrameCache *frameCache = SpriteFrameCache::getInstance();
frameCache->addSpriteFramesWithFile("boy.plist", "boy.png");
//从SpriteFrameCache缓存中找到boy1.png这张图片.
auto frame_sp = Sprite::createWithSpriteFrameName("boy1.png");
this->addChild(frame_sp, 2);
//// // 从精灵帧缓存中删除一个精灵帧. SpriteFrameCache::getInstance()->removeSpriteFrameByName(const std::string &name); // 清除载入精灵帧的字典。如果接收到“Memory Warning”, 请调用这个方法。 // 就眼前来说 : 它将释放一些资源来阻止你的应用崩溃掉。 // 中期的角度 : 它将分配更多的资源。 // 从长远来说 : 它将变成相同的。 SpriteFrameCache::getInstance()->removeSpriteFrames(); // 从一个.plist文件移除多个精灵帧。即:存储在这个文件的精灵帧将被删除。 // 当某个特定的纹理需要被删除时候调用这个方法很方便。 SpriteFrameCache::getInstance()->removeSpriteFramesFromFile(const std::string &plist); // 移除与特定的纹理结合的所有的精灵帧。 // 当某个特定的纹理需要被删除时候调用这个方法很方便。 SpriteFrameCache::getInstance()->removeSpriteFramesFromTexture(cocos2d::Texture2D *texture); // 移除没用的精灵帧。保留数为1的精灵帧将被删除。 // 在开始一个新的场景之后调用这个方法很方便。 SpriteFrameCache::getInstance()->removeUnusedSpriteFrames(); //
// // 添加一个动画到缓存,命名为name。 // name - animation : 是一组 键-值对(key-value) 的关系。 void addAnimation(Animation *animation, const std::string& name); // 添加动画的plist文件到缓存 void addAnimationsWithFile(const std::string& plist); // 获得指定名称为name的动画 Animation* getAnimation(const std::string& name); // 移除一个指定的动画 void removeAnimation(const std::string& name); //
//
//
//动画命名为 Explosion,加入到动画缓存中
Animation* animation = Animation::createWithSpriteFrames(arr, 0.04);
AnimationCache::getInstance()->addAnimation(animation, "Explosion");
//直接从动画缓存中取出 "Explosion" 动画
Animation* animation = AnimationCache::getInstance()->getAnimation("Explosion");
//
//标签:
原文地址:http://blog.csdn.net/u013517637/article/details/45647387