From 4f4c30051ee002a7c4d331026731044f9a278fa4 Mon Sep 17 00:00:00 2001 From: lite3 Date: Mon, 8 Dec 2014 21:04:15 +0800 Subject: [PATCH] retain the sprite frame do not removed by SpriteFrameCache::removeUnusedSpriteFrames --- cocos/2d/CCSprite.cpp | 10 ++++++++++ cocos/2d/CCSprite.h | 1 + 2 files changed, 11 insertions(+) diff --git a/cocos/2d/CCSprite.cpp b/cocos/2d/CCSprite.cpp index 34653d36b3..5f491747b9 100644 --- a/cocos/2d/CCSprite.cpp +++ b/cocos/2d/CCSprite.cpp @@ -264,6 +264,7 @@ Sprite::Sprite(void) : _batchNode(nullptr) , _shouldBeHidden(false) , _texture(nullptr) +, _spriteFrame(nullptr) , _insideBounds(true) { #if CC_SPRITE_DEBUG_DRAW @@ -274,6 +275,7 @@ Sprite::Sprite(void) Sprite::~Sprite(void) { + CC_SAFE_RELEASE(_spriteFrame); CC_SAFE_RELEASE(_texture); } @@ -936,6 +938,14 @@ void Sprite::setSpriteFrame(const std::string &spriteFrameName) void Sprite::setSpriteFrame(SpriteFrame *spriteFrame) { + // retain the sprite frame + // do not removed by SpriteFrameCache::removeUnusedSpriteFrames + if (_spriteFrame != spriteFrame) + { + CC_SAFE_RELEASE(_spriteFrame); + _spriteFrame = spriteFrame; + spriteFrame->retain(); + } _unflippedOffsetPositionFromCenter = spriteFrame->getOffset(); Texture2D *texture = spriteFrame->getTexture(); diff --git a/cocos/2d/CCSprite.h b/cocos/2d/CCSprite.h index 226eb4d55b..29c152c3bf 100644 --- a/cocos/2d/CCSprite.h +++ b/cocos/2d/CCSprite.h @@ -538,6 +538,7 @@ protected: // BlendFunc _blendFunc; /// It's required for TextureProtocol inheritance Texture2D* _texture; /// Texture2D object that is used to render the sprite + SpriteFrame* _spriteFrame; QuadCommand _quadCommand; /// quad command #if CC_SPRITE_DEBUG_DRAW DrawNode *_debugDrawNode;