2013-10-12 116 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];