2013-09-29 41 views
1

我想通过使用UIRrotationGestureRecognizer旋转SKSpriteNode。我已经实现了一个代码,但有时候,当我旋转节点时,它跳转到一个不是它应该是的那个旋转。在这里,你有代码:SKSpriteNode旋转不正常

- (void) handleRotation:(UIRotationGestureRecognizer *) rotationrecognizer{ 

    CGFloat initialrotation = 0.0; 

    if (rotationrecognizer.state == UIGestureRecognizerStateBegan) { 

     CGPoint touchLocation = [rotationrecognizer locationInView:rotationrecognizer.view]; 
     touchLocation = [self convertPointFromView:touchLocation]; 
     [self selectNodeForTouch:touchLocation]; 

     initialrotation = selected.zRotation; 
    } 

    else if (rotationrecognizer.state == UIGestureRecognizerStateChanged) {   

     CGFloat angle = initialrotation + rotationrecognizer.rotation; 
     selected.zRotation = angle;  

    } 
} 
+0

考虑到我确实已经解决了您的代码的一个根本问题,您将不胜感激并接受我的答案。 – MobileVet

回答

3

您在每次调用重置初始旋转0 ...你应该,如果你需要它驻留移动这是您的观点的伊娃。

你现在写的方式,即设置“角度”的线是有效等于这个:

CGFloat angle = 0 + rotationrecognizer.rotation; 

相反,你应该做到以下(其中initialRotation初始定义为私人伊娃):

- (void) handleRotation:(UIRotationGestureRecognizer *) rotationrecognizer{ 

    if (rotationrecognizer.state == UIGestureRecognizerStateBegan) { 

     CGPoint touchLocation = [rotationrecognizer locationInView:rotationrecognizer.view]; 
     touchLocation = [self convertPointFromView:touchLocation]; 
     [self selectNodeForTouch:touchLocation]; 

     _initialrotation = selected.zRotation; 
    } 

    else if (rotationrecognizer.state == UIGestureRecognizerStateChanged) {   

     CGFloat angle = _initialrotation + rotationrecognizer.rotation; 
     selected.zRotation = angle;  

    } 
} 
+0

我已经完成了你所说的话,并且在某些情况下,精灵会不断旋转... –

+0

你能再详细描述一下他们在某些情况下如何回应'坏''吗? 他们曾经如何操作你的期望?什么是'坏'?你打印出你的旋转值吗?它们有意义吗? – MobileVet

+0

我的意思是说它可能旋转了70º,然后当我旋转一点时它跳到200º。但我没有申请130º旋转。 –