Cocos2D中。在没有硬数学计算的情况下围绕另一个点旋转点?

时间:2012-05-21 07:58:26

标签: cocos2d-iphone rotation geometry angle

我还没有看到关于特定点周围旋转的文章示例。我试过这样的东西,但它不起作用:

//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 个答案:

答案 0 :(得分:2)

以下是如何围绕特定点P(50,50)旋转节点(精灵等),其半径(距离P)为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];

答案 1 :(得分:0)

我的近似解决方案:

@interface Bomb : NSObject {
    CCSprite *center;
}

...

@end

和一些方法:

- (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);
}

当然我应该添加精灵删除。