加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 04:55:54 所属栏目:百科 来源:网络整理
导读:我正在尝试将玩家旋转到触摸状态.使用触摸移动功能,我将位置设置为变量.然后我调用一个函数.它工作但角度是关闭的.位置是全局变量,玩家是我要转的精灵.大约15度. func rotatePlayer(){ let angle = atan2(location!.y - player!.position.y,location!.x - pl
我正在尝试将玩家旋转到触摸状态.使用触摸移动功能,我将位置设置为变量.然后我调用一个函数.它工作但角度是关闭的.位置是全局变量,玩家是我要转的精灵.大约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图像,然后选择“在外部编辑器中打开”菜单项.这将在预览应用程序中打开图像.在预览中,选择工具>向右旋转并退出预览.通过旋转图稿,您的代码应该无需任何更改即可运行.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读