Reputation: 9596
how can I find the center point of a CCSprite?
My pseudocode is:
A get sprite bounding box
B compute center point of the sprite
C move center point relative to sprite position and anchor point (this will be the center point of the sprite)
And my pseudosolution is so far like this:
A [sprite boundingbox] B How to find the Center Coordinate of Rectangle? C I don't know :)
Any suggestion?
Upvotes: 0
Views: 3324
Reputation: 191
By default the Anchor point of any sprite is it's center point and simply by writing mySprite.position will give the center point in terms of CGPoint.You can find out x and y coordinate using mySprite.position.x and mySprite.position.y.Also you can shift the anchor point of any sprite from its center to any desired point by using code
mySprite.setAnchorPoint=ccp(0,0);// left down corner as anchor point here
You can get and print center point of any sprite as
NSLog("x=%d ,y=%d",mySprite.position.x,mySprite.position.y);
Upvotes: 0
Reputation: 7850
CGPoint spriteCenter = CGPointApplyAffineTransform(ccp(sprite.contentSize.width/2, sprite.contentSize.height/2),
sprite.nodeToParentTransform);
Upvotes: 2
Reputation: 64477
Center point of a sprite:
sprite.position;
This is assuming the anchorPoint is at its default, which is 0.5,0.5 and highly recommended not to change it because it gets you in exactly this situation you're in right now.
Instead of changing the sprite's anchorPoint, add the sprite to a CCNode object as child and offset the sprite from the CCNode. From then on move or reposition the sprite by changing the node's position property.
Upvotes: 5