That\'s the question xD
Given an instance of a CCSprite in cocos2d in iphone, what method can I use to obtain the image width and height?
The other answers are incomplete and out-of-date.
Note that I'm using JavaScript below along with destructuring assignment syntax. Be sure to view the Cocos API documentation for your language implementation.
Gives you the:
setScale()
is applied to the sprite).anchorPoint
for sprites is (0.5, 0.5), while this coordinate represents the (0, 0) position. In other words, if the anchorPoint is set at the default, then getBoundingBox().x
+ getBoundingBox().width
/ 2 = getPosition().x
(the x value you set in setPosition()
).Example:
const boundingBox = sprite.getBoundingBox();
const { x, y, width, height } = boundingBox;
Gives you the:
Example:
const contentSize = sprite.getContentSize();
const { x, y } = contentSize;
Gives you the:
Example:
const textureRect = sprite.getTextureRect();
const { x, y, width, height } = textureRect;