iPhone Cocos2D -手指旋转精灵



我已经子类化了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。我还添加了委托方法shouldrecognizesimultanouslywithgesturerecognizer,并将其设置为YES。

在做了这些并检查之后,捏和平移手势就像地狱一样快。另一方面,旋转继续缓慢。添加另外两个手势识别器并没有降低旋转速度。其他两个响应流畅快速,UIRotationGestureRecognizer慢。

手势旋转以弧度为单位,而Cocos2D旋转以度为单位。因此,您需要按照如下方式进行转换:

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

你也可以省去这些麻烦并使用Kobold2D,它不仅为Cocos2D添加了一个易于使用的手势(和其他输入类型)界面,而且还将值相应地转换为Cocos2D视图坐标和角度。您再也不用考虑转换值了。

最新更新