【问题标题】:iPhone Cocos2D - finger rotating a spriteiPhone Cocos2D - 手指旋转精灵
【发布时间】:2011-12-27 13:19:42
【问题描述】:

我已经继承了 CCSprite 类并添加了一个 UIRotationGestureRecognizer 到它。所以,在我的 init 方法中我有这个

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

然后我就有了方法

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

    float rotation = [recognizer rotation];
    self.rotation += rotation;
}

它工作得很好,但在实际手势和旋转本身之间有很大的滞后。我想说手势和精灵响应之间几乎有 0.5 秒。

我该如何解决?谢谢。


注意:在第一条评论之后,我向精灵添加了另外两个识别器:UIPinchGestureRecognizer 和 UIPanGestureRecognizer。我还添加了委托方法 shouldRecognizeSimultaneouslyWithGestureRecognizer 并将其设置为 YES。

完成此操作并检查后,捏合和平移手势非常快。另一方面,旋转继续缓慢。添加这两个其他手势识别器并没有降低旋转速度。另外两个响应流畅且快速,UIRotationGestureRecognizer 慢。

【问题讨论】:

  • 您是否在视图上设置了任何其他手势识别器?
  • 没有。你说的视图是什么意思?精灵?这是我的一个问题。当使用 [[[CCDirector sharedDirector] openGLView] 添加gestureRecognizer 时,这个openGLView 对我来说听起来像是拥有所有图形的主openGLView。这不应该只设置为精灵吗?如果是这样,我该怎么做?
  • 我的意思是正确的观点openGLView。将手势识别器附加到该视图(如您所说,主视图)是正确的,因为它是您拥有的唯一视图。我也这样做,它不会增加任何延迟。如果您使用更多的 gest。识别器,那么可能会有一些交互,我想
  • 我已编辑问题以提供更多信息。我为同一个精灵添加了另外两个识别器:pinch 和 pan。 Pinch 和 Pan 响应快速而流畅,但 UIRotateGestureRecognizer 却没有...... :(

标签: iphone ios ipad cocos2d-iphone uigesturerecognizer


【解决方案1】:

手势旋转以弧度为单位,而 Cocos2D 旋转以度为单位。因此,您需要将其转换如下:

- (void)handleRotation:(UIRotationGestureRecognizer *)recognizer 
{
    float rotation = CC_RADIANS_TO_DEGREES([recognizer rotation]);
    self.rotation += rotation;
}

你也可以省去这些麻烦,使用 Kobold2D,它不仅在 Cocos2D 中添加了easy to use interface for gestures(和其他输入类型),还可以将值相应地转换为 Cocos2D 视图坐标和度数。您永远不必再考虑转换值。

【讨论】:

  • ahhhhhhhh,这解释了很多!极好的!谢谢。你是 Cocos2D 的高手!!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多