diff --git a/cocos/2d/CCSprite.cpp b/cocos/2d/CCSprite.cpp index a98e65105d..caa06dbe50 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;