我目前正在使用此代码旋转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,但它导致了同样的问题。
提前致谢
答案 0 :(得分: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]]
答案 1 :(得分: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];