2012-05-21 18 views
0

我还没有看到有关在指定点周围旋转的简单示例。我想是这样的,但它不工作:Cocos2d。绕另一点旋转点而不需要进行数学计算?

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

回答

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

当然我应该加精灵删除。

2

下面是可以旋转的节点(精灵等),围绕某一点P(50,50)与100为半径(与P的距离) :

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]; 
+0

我必须添加“中心”变量作为孩子吗? – Gargo

+0

当然,您创建但不作为子项添加的任何节点都将从内存中释放。和它的所有孩子一起。 – LearnCocos2D

+0

我试过了。如果你想简单地旋转精灵,它是完美的。但碰撞检测有很多问题,因为有些精灵使用绝对位置,有些使用相对位置。 – Gargo