Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Sprite Anchorpoint without moving it?

I am trying to change my Sprite anchor point so that I can rotate over a 0.0f,0.0f anchorpoint. At first my object is rotation at the default anchor point (0.5f,0.5f). However later on I need it to rotate over a 0.0,0.0 AnchorPoint.

The problem is I cannot change the anchor point and change the position accordingly, so it stays on the same position, without the object appearing to quickly move and reposition to its original point.

Is there a way I can set the anchor point and the position of my Sprite at once, without it moving at all?. Thank you.

-Oscar

like image 568
Oscar Gomez Avatar asked Jan 18 '10 21:01

Oscar Gomez


People also ask

How do I add a pivot point to a Sprite?

Locating your pivot point is pretty easy. Just select one of the sprites. Click the circle in the middle that designates the pivot point and drag it to the location you prefer. Next make not of the pivot point settings in the Custom Pivot section at the bottom right of the Sprite Editor.


1 Answers

I found a solution to this with a UIView elsewhere, and rewrote it for cocos2d:

- (void)setAnchorPoint:(CGPoint)anchorPoint forSprite:(CCSprite *)sprite
{
    CGPoint newPoint = CGPointMake(sprite.contentSize.width * anchorPoint.x, sprite.contentSize.height * anchorPoint.y);
    CGPoint oldPoint = CGPointMake(sprite.contentSize.width * sprite.anchorPoint.x, sprite.contentSize.height * sprite.anchorPoint.y);

    newPoint = CGPointApplyAffineTransform(newPoint, [sprite nodeToWorldTransform]);
    oldPoint = CGPointApplyAffineTransform(oldPoint, [sprite nodeToWorldTransform]);

    CGPoint position = sprite.position;

    position.x -= oldPoint.x;
    position.x += newPoint.x;

    position.y -= oldPoint.y;
    position.y += newPoint.y;

    sprite.position = position;
    sprite.anchorPoint = anchorPoint;
}
like image 123
CeeBee Avatar answered Oct 05 '22 22:10

CeeBee