diff --git a/core/2d/Sprite.cpp b/core/2d/Sprite.cpp index 069fa30bb47e..ff6a2a9ccb2b 100644 --- a/core/2d/Sprite.cpp +++ b/core/2d/Sprite.cpp @@ -415,7 +415,10 @@ void Sprite::setTextureRect(const Rect& rect, bool rotated, const Vec2& untrimme { _rectRotated = rotated; - Node::setContentSize(untrimmedSize); + if (_autoSizeEnabled || _contentSize == Vec2::ZERO) + { + Node::setContentSize(untrimmedSize); + } _originalContentSize = untrimmedSize; setVertexRect(rect); diff --git a/core/2d/Sprite.h b/core/2d/Sprite.h index 744048045a8e..d234c6f6ffa4 100644 --- a/core/2d/Sprite.h +++ b/core/2d/Sprite.h @@ -625,6 +625,14 @@ class AX_DLL Sprite : public Node, public TextureProtocol void setAutoUpdatePS(bool bVal) { _autoUpdatePS = bVal; } + /** + * Indicate if the sprite content size can change if new textures are applied to + * the sprite. + * + * @param enabled True if the sprite can change size on new frames/textures + */ + void setAutoSize(bool enabled) { _autoSizeEnabled = enabled; } + protected: virtual void updateColor() override; virtual void setTextureCoords(const Rect& rect); @@ -703,6 +711,8 @@ class AX_DLL Sprite : public Node, public TextureProtocol bool _stretchEnabled = true; bool _autoUpdatePS = true; + bool _autoSizeEnabled = true; + private: AX_DISALLOW_COPY_AND_ASSIGN(Sprite); };