用触摸旋转一个精灵 - Cocos2d

问题描述:

我知道这个问题已经被问了好几次了,相信我我已经搜索过了。我发现了一个用触摸旋转精灵的答案,但必须有一个更简单的方法。用触摸旋转一个精灵 - Cocos2d

我需要的是让我的精灵随着我的触摸旋转。最大旋转0最小旋转0.

我知道我需要一些检查。

int maxRot = 0; 
int minRot = 0; 

    if (arrowRotation > maxRot) 
    { 
    //do or don't do something 
    } else if (arrowRotation < minRot) 
    { 
    //do or don't do something 
    } 

有人能带我以正确的方向旋转一个精灵,触摸,最小和最大旋转?

这里是我认为是复杂或可以用更简单的方式完成的代码。

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 

    //acquire the previous touch location 
    CGPoint firstLocation = [touch previousLocationInView:[touch view]]; 
    CGPoint location = [touch locationInView:[touch view]]; 

    //preform all the same basic rig on both the current touch and previous touch 
    CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location]; 
    CGPoint firstTouchingPoint = [[CCDirector sharedDirector] convertToGL:firstLocation]; 

    CGPoint firstVector = ccpSub(firstTouchingPoint, _arrow.position); 
    CGFloat firstRotateAngle = -ccpToAngle(firstVector); 
    CGFloat previousTouch = CC_RADIANS_TO_DEGREES(firstRotateAngle); 

    CGPoint vector = ccpSub(touchingPoint, _arrow.position); 
    CGFloat rotateAngle = -ccpToAngle(vector); 
    CGFloat currentTouch = CC_RADIANS_TO_DEGREES(rotateAngle); 

    //keep adding the difference of the two angles to the dial rotation 
    arrowRotation += currentTouch - previousTouch; 
} 

在此先感谢!

+1

添加链接到你认为太复杂的解决方案 - 这可能是唯一的方法;) – deanWombourne

+0

我用代码更新了我的问题! – Jonathan

+0

你的意思是maxRot和minRot都是= 0。 – KDaker

我不认为你可以简化到任何简单的旋转。我真的没有得到你想如何最小和最大旋转为零。它们不能具有相同的价值。如果你想限制旋转至最大和最小以下内容添加到您的ccTouchesMoved:方法的末尾:

if (arrowRotation >= maxRot) { 

    arrowRotation = maxRot; 
} 

else if (arrowRotation <= minRot) { 

    arrowRotation = minRot; 
} 
+0

只是示例中的最小和最大旋转。他们两个不会是0. – Jonathan

使用KTOneFingerRotationGestureRecognizer ...

KTOneFingerRotationGestureRecognizer *rotation; 

rotation = [[KTOneFingerRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotating:)]; 
[[self Wheel] addGestureRecognizer:rotation]; 
[rotation release]; 

方法

- (void)rotating:(KTOneFingerRotationGestureRecognizer *)recognizer { 
    view = [recognizer view]; 
    if(isRotating == FALSE) { 
     [view setTransform:CGAffineTransformRotate([view transform], [recognizer rotation])]; 
    } 
} 
+1

我无法设置转换为CCsprite。 – Jonathan