微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用Swift将精灵旋转到精灵位置不精确的SpriteKit中

我正在尝试将玩家旋转到触摸状态.使用触摸移动功能,我将位置设置为变量.然后我调用一个函数.它工作但角度是关闭的.位置是全局变量,玩家是我要转的精灵.大约15度.

func rotatePlayer(){

     let angle = atan2(location!.y - player!.position.y,location!.x - player!.position.x)
    player?.zRotation = angle
}

解决方法

根据 SpriteKit Best Practices doc

Use game logic and art assets that match SpriteKit’s coordinate and rotation conventions. This means orienting artwork to the right. If you orient the artwork in some other direction,you need to convert angles between the conventions used by the art and the conventions used by SpriteKit.

由于认宇宙飞船向上指向(当zRotation为0时),您需要将角度偏移90度(pi / 2弧度),以便当zRotation为零时船舶朝向右侧:

player?.zRotation = angle - CGFloat(M_PI_2)

或者,您可以将太空船旋转到右侧.要旋转图像,请单击Assets.xcassets,然后单击Spaceship.右键单击Spaceship图像,然后选择“在外部编辑器中打开”菜单项.这将在预览应用程序中打开图像.在预览中,选择工具>向右旋转并退出预览.通过旋转图稿,您的代码应该无需任何更改即可运行.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐