0

我还没有看到关于围绕指定点旋转的简单示例。我尝试了这样的事情,但它不起作用:

//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);
}
4

2 回答 2

2

以下是如何围绕半径(到 P 的距离)为 100 的某个点 P (50,50) 旋转节点(精灵等)的方法:

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];
于 2012-05-21T08:48:34.813 回答
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);
}

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

于 2012-05-28T07:14:51.710 回答