0

I haven't seen easy examples about rotaion around a specified point. I tried something like this but it doesn't work:

//CCNode *node is declared
//In a function of a subclass of CCSprite
- (void)moveWithCicrlce
{
anchorNode = [CCNode node];
anchorNode.position = ccpSub(self.position, circleCenter);
anchorNode.anchorPoint = circleCenter;
[anchorNode runAction:[CCRotateBy actionWithDuration:1 angle:90]];
[self runAction:[CCRepeatForever actionWithAction:[CCSequence actions:[CCCallFunc actionWithTarget:self selector:@selector(rotate)], [CCDelayTime actionWithDuration:0.1], nil]]];
}

- (void)rotate
{
self.position = ccpAdd(anchorNode.position, anchorNode.anchorPoint);
}

2 Answers 2

2

Here's how you can rotate a node (sprite etc) around a certain point P (50,50) with a radius (distance from P) of 100:

CCNode* center = [CCNode node];
center.position = CGPointMake(50, 50);
[self addChild:center];

// node to be rotated is added to center node
CCSprite* rotateMe = [CCSprite spriteWithFile:@"image.png"];
[center addChild:rotateMe];

// offset rotateMe from center by 100 points to the right
rotateMe.position = CGPointMake(100, 0);

// perform rotation of rotateMe around center by rotating center
id rotate = [CCRotateBy actionWithDuration:10 rotation:360];
[center runAction:rotate];
4
  • must I add the "center" variable as child?
    – Gargo
    May 22, 2012 at 8:29
  • Of course, any node you create but don't add as child will be released from memory. And all of its children along with it.
    – CodeSmile
    May 22, 2012 at 22:51
  • I tried it. It works perfect if you want to simply rotate the sprite. But there are a lot of problems with collision detection, because some sprites use absolute positions and some use relative positions.
    – Gargo
    May 25, 2012 at 8:58
  • I think I almost have found a solution, but I don't know how get an absolute sprite position using its own position, size and anchor point.
    – Gargo
    May 25, 2012 at 10:11
0

My approximate solution:

@interface Bomb : NSObject {
    CCSprite *center;
}

...

@end

and some methods:

- (void)explode
{
    BombBullet *bullet = [BombBullet spriteWithFile:@"explosion03.png"];
    [[[CCDirector sharedDirector] runningScene] addChild:bullet];

    center = [CCSprite spriteWithTexture:bullet.texture];
    center.position = explosionPoint;
    center.anchorPoint = ccp(-0.5, -0.5);
    center.visible = NO;
    [[[CCDirector sharedDirector] runningScene] addChild:center];
    [center runAction:[CCRotateBy actionWithDuration:1 angle:360]];

    CCCallFunc *updateAction = [CCCallFuncN actionWithTarget:self selector:@selector(update:)];
    [bullet runAction:[CCRepeatForever actionWithAction:[CCSequence actions:updateAction, [CCDelayTime actionWithDuration:0.01], nil]]];
}

- (void)update:(id)sender
{
    BombBullet *bombBullet = (BombBullet *)sender;
    bombBullet.rotation = center.rotation;
    bombBullet.position = ccpAdd(center.position, center.anchorPointInPoints);
    bombBullet.position = ccpAdd(bombBullet.position, ccp(-bombBullet.contentSize.width / 2, -bombBullet.contentSize.height / 2));
    bombBullet.position = ccpRotateByAngle(bombBullet.position, center.position, bombBullet.rotation);
}

of course I should add sprite deleting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.