2013-10-12 162 views
0

我目前使用此代碼來旋轉CCSprite(播放器)對象以面對最後一次觸摸。問題是這使得旋轉變得非常激動,並且看起來不太平滑。旋轉CCSprite以在Cocos2D中觸摸

CGPoint playerPos = [player position]; 
CGPoint diff = CGPointMake(currentPoint.x-lastPoint.x, currentPoint.y-lastPoint.y); 
CGPoint playerNewPos = ccpAdd(playerPos, diff); 
[player setRotation:-CC_RADIANS_TO_DEGREES(atan2(playerNewPos.y-playerPos.y, playerNewPos.x-playerPos.x))]; 

我怎樣才能讓這段代碼更流暢流暢?

我也嘗試使用CCRotateTo但它導致相同的問題。

在此先感謝

回答

0

試試這個:

float angle = -CC_RADIANS_TO_DEGREES(atan2(playerNewPos.y-playerPos.y, playerNewPos.x-playerPos.x)); 
id action=[CCRotateTo actionWithDuration:1.0 angle:angle]; 
[player runAction:[CCEaseInOut actionWithAction:action rate:3]] 
+0

@Viktor Lexington謝謝你指出這一點。 –

0

這裏得到舊的和新的觸摸位置,如下

- (void)registerWithTouchDispatcher 
{ 
    [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate: 
      self priority:0 swallowsTouches:YES]; 
} 

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    return YES; 
} 

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event 
{ 

    CGPoint touchLocation = [self convertTouchToNodeSpace:touch]; 

    NSLog(@"touch end ==%f-----%f",touchLocation.x,touchLocation.y); 

    // Save old location to NSuserDefault And get new From Above 

} 

根據位置,如下現在旋轉

int offX = (CurrentTouch.x - OldTouch.x); 
int offY = (CurrentTouch.y - OldTouch.y)); 

float angleRadians = atanf((float)offX/(float)offY); 
float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians); 

id action=[CCRotateTo actionWithDuration:1.0 angle:angleDegrees]; 
[player runAction:action];