iphone - 让 Sprite 平滑地跟踪接触点

标签 iphone ios xcode cocos2d-iphone

我正在尝试制作我的 sprite 脸并在它移动时跟随一个接触点,到目前为止我已经成功了。基本上,每当用户触摸屏幕时,我的 Sprite 就会旋转以面对触摸点,并向它移动。这是我的基本代码:

-(void)handlePan:(UIPanGestureRecognizer *)recognizer{

    //calculate tapPosition + angle, then rotate the sprite and move it to the tapPosition:

    [fish runAction:[CCRotateTo actionWithDuration:0 angle:angle]];
    [fish runAction:[CCMoveTo actionWithDuration:0.8 position:tapPosition]];
}

现在的问题是,由于这是在 UIPanGesture 中,只要手指触摸屏幕,此方法就会不断运行。这实际上使旋转和移动非常不稳定,因为以前的 Action 总是被新的 Action 覆盖。

它不流畅。我怎样才能使它光滑?我是否需要以某种方式延迟我的某些行为或其他事情?任何想法表示赞赏。谢谢

最佳答案

当你需要不断改变方向和 Action 时,不要使用 Action 。 Action 对此不利,甚至可能导致您的对象在每帧停止/运行新 Action 时根本不动。

相反,您必须逐帧计算对象的速度并将该速度添加到位置。您还可以根据速度计算角度(方向也称为旋转)。

关于iphone - 让 Sprite 平滑地跟踪接触点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10632529/

相关文章:

iPhone - 如何将可用的新应用程序版本同步到应用程序商店和数据库升级?

ios - NSUserDefaults和原始数据类型?

ios - 构建应用程序时找不到 Cocoapods header

xcode - Cocos2d 2.1和Xcode7 iOS9崩溃着色器

ios - Xcode 4.1 fatal error : stdlib modified since the precompiled header was built

iOS为uitableviewcell添加自定义属性

ios - application : didReceiveRemoteNotification: fetchCompletionHandler: called more than once. 如何避免?

ios - UIView : detecting scroll programmatically 中的 UIScrollView

iphone - 如何解决 -[__NSCFString JSONValue] : unrecognized selector sent to instance when application terminates?

iOS:文本字段委托(delegate),如何在 TableView 中处理行移动之前自动结束编辑?