简体   繁体   English

旋转CCSprite触摸Cocos2D

[英]Rotate CCSprite to touch in Cocos2D

I'm currently using this code to rotate a CCSprite (player) object to face the last touch. 我目前正在使用此代码旋转CCSprite(玩家)对象以面对最后的触摸。 The problem is this makes the rotation pretty jumpy, and it doesn't look very smooth. 问题在于这使旋转变得非常跳跃,而且看起来也不是很平滑。

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))];

How could I make this code more smooth and fluid? 如何使此代码更流畅,更流畅?

I've also tried using CCRotateTo but it's causing the same issue. 我也尝试过使用CCRotateTo,但这会导致相同的问题。

Thanks in advance 提前致谢

try this: 尝试这个:

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]]

Here get Old and New Touch Position as below 这里获得旧的和新的触摸位置如下

- (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

}

Now Rotate according to Position as below 现在根据以下位置旋转

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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM