Blog

COCOS2D: HOW TO MOVE ALONG THE CIRCLE WITHOUT FUNKY CCBEZIERBY

I got a problem to make a Cocos2D animation where some sprites have to move along a circle. Quick googling gave me some links on advices to use CCBezierTo. But I found it to be extremely complex for my task:)

I found easier way. Idea is very simple. You can use CCRotateBy… Put your sprite in the center of the circle, shift it on its orbit by setting its anchor point, adjust exact position with YourSpriteObj.rotation property.

And here is a little snippet of the code:

// Create some sprite

CCSprite* sprite = [CCSprite spriteWithSpriteFrameName:@”SripteFileID”];

[parentNode addChild:sprite z:1 tag:200];

// Put it somewhere you need

sprite.position = ccp(248, 162);

sprite.anchorPoint = ccp(2.69, 0.5);// changing position actually – but measuring with sprite size

sprite.rotation = 45;

//At last rotate, in real life everything a little bit more complex :p

id rotate = [CCRotateBy actionWithDuration:1.0f angle:45*n];

[sprite runAction:rotate];