2011-09-20 114 views
1

我知道這個問題已經被問了好幾次了,相信我我已經搜索過了。我發現了一個用觸摸旋轉精靈的答案,但必須有一個更簡單的方法。用觸摸旋轉一個精靈 - 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

回答

0

我不認爲你可以簡化到任何簡單的旋轉。我真的沒有得到你想如何最小和最大旋轉爲零。它們不能具有相同的價值。如果你想限制旋轉至最大和最小以下內容添加到您的ccTouchesMoved:方法的末尾:

if (arrowRotation >= maxRot) { 

    arrowRotation = maxRot; 
} 

else if (arrowRotation <= minRot) { 

    arrowRotation = minRot; 
} 
+0

只是示例中的最小和最大旋轉。他們兩個不會是0. – Jonathan

0

使用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