使用 UIRotationGestureRecognizer Cocos2D-iPhone CCSprite 旋转速度非常快

Posted

技术标签:

【中文标题】使用 UIRotationGestureRecognizer Cocos2D-iPhone CCSprite 旋转速度非常快【英文标题】:CCSprite Rotation Speed is very fast using UIRotationGestureRecognizer Cocos2D-iPhone 【发布时间】:2013-11-01 06:54:12 【问题描述】:

我正在使用以下代码旋转精灵,但旋转速度非常快,并且与手指的旋转不同步。

在init方法中我写了如下代码

UIRotationGestureRecognizer* rot = [[UIRotationGestureRecognizer alloc] initWithTarget:self action: @selector (rotateSelector:)];
[[[CCDirector sharedDirector] openGLView] addGestureRecognizer:rot];

选择器实现如下。

- (void)rotateSelector: (UIRotationGestureRecognizer*) gestureRecognizer
    
        rotation = CC_RADIANS_TO_DEGREES([gestureRecognizer rotation]);
        mySprite.rotation += rotation;

    

我认为问题出在以下行 mySprite.rotation += 旋转;

但我想不通,如何使 CCSprite 的旋转与手指的旋转同步。

【问题讨论】:

指定旋转,不要添加 如果我不添加它,那么下次我旋转精灵时它会从其原始位置开始......在这种情况下速度很慢但不是从最后一个旋转角度开始。 【参考方案1】:

不要将手势旋转添加到精灵的旋转属性中,这是您不想要的,添加旋转差异将为您提供您想要的结果。喜欢:

if (recognizer.state == UIGestureRecognizerStateBegan)

    self.lastRotAngle = CC_RADIANS_TO_DEGREES([recognizer rotation]);

else if (recognizer.state == UIGestureRecognizerStateChanged)

    float rotation = CC_RADIANS_TO_DEGREES([recognizer rotation]);

    sprite.rotation += rotation - self.lastRotAngle;
    self.lastRotAngle = rotation;

我希望这会有所帮助。

【讨论】:

以上是关于使用 UIRotationGestureRecognizer Cocos2D-iPhone CCSprite 旋转速度非常快的主要内容,如果未能解决你的问题,请参考以下文章