iPhone Cocos2D - 手指旋转精灵

发布于 2024-12-15 06:27:44 字数 909 浏览 4 评论 0原文

我对 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 则慢。

I have subclassed CCSprite class and added a UIRotationGestureRecognizer to it. So, in my init method I have this

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

and then I have the method

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

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

it works perfectly but it has a huge lag between the actual gesture and the rotation itself. I would say almost 0.5 seconds between the gesture and the sprite response.

How do I solve that? thanks.


NOTE: After the first comment, I have added two more recognizers to the sprite: UIPinchGestureRecognizer and UIPanGestureRecognizer. I have also added the delegate method shouldRecognizeSimultaneouslyWithGestureRecognizer and set that to YES.

After doing this and checking, pinch and pan gestures are fast as hell. On the other hand, rotation continues slow. There was not reduction on the rotation speed by adding these two other gestures recognizer. The other two respond fluid and fast, UIRotationGestureRecognizer is slow.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必了 2024-12-22 06:27:44

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

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

您也可以省去这些麻烦并使用 Kobold2D,它不仅添加了 易于使用的手势界面(和其他输入类型)到 Cocos2D,但也会相应地将值转换为Cocos2D 视图坐标和度数。您永远不必再考虑转换值。

Gesture rotation is in radians while Cocos2D rotation is in degrees. So you need to convert this as follows:

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

You can also save yourself these troubles and use Kobold2D, which not only adds an easy to use interface for gestures (and other input types) to Cocos2D but also converts the values accordingly to Cocos2D view coordinates and degrees. You'll never have to think about converting values again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文