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

"center"변수를 자식으로 추가해야합니까? – Gargo

+0

물론 생성했지만 노드로 추가하지 않은 노드는 메모리에서 해제됩니다. 그리고 그것과 함께 모든 아이들. – LearnCocos2D

+0

나는 그것을 시험해 보았다. 스프라이트를 단순히 회전 시키려면 완벽하게 작동합니다. 그러나 일부 Sprite는 절대 위치를 사용하고 일부 Sprite는 상대 위치를 사용하기 때문에 충돌 감지에 많은 문제가 있습니다. – Gargo