0

我目前正在使用此代码旋转 CCSprite(播放器)对象以面对最后一次触摸。问题是这使旋转变得非常跳跃,并且看起来不是很平滑。

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 ,但它导致了同样的问题。

提前致谢

4

2 回答 2

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]]
于 2013-10-12T10:58:23.147 回答
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];
于 2013-10-15T05:48:19.720 回答