2012-05-21 38 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