ios - Swift SpriteKit - SKAction 中的 zRotation 跟随路径

标签 ios sprite-kit rotation uibezierpath skaction

我正在开发一款游戏,现在我对沿着路径时 SpriteNode 的 zRotation 有疑问。

所以我有一个看起来像这样的 ShapeNode: enter image description here

我为此形状使用 UIBezierPath。

现在我有一个 SpriteNode,它从这条路径的中间开始,从左到右随机移动,否则。

为此我有两个功能:

func FischRechtsBewegen(){ //for moving right
    FischNode1.removeAllActions()
    let BewegeRechts = SKAction.follow(FischKurveRechts.cgPath, asOffset: false, orientToPath: true, duration: 6)
    FischNode1.run(BewegeRechts)
}

func FischLinksBewegen(){ //for moving left
    FischNode1.removeAllActions()
    let BewegeLinks = SKAction.follow(FischKurveLinks.cgPath, asOffset: false, orientToPath: true, duration: 6)
    FischNode1.run(BewegeLinks)
}

同样在这个函数中,我为 SpriteNode 的每个方向变化计算一条新路径。 (但由于这段代码对我的问题并不重要,所以我在此处的示例代码中删除了它)

这条新路径(对于 SKAction)从 SpriteNode 的实际位置开始,然后向右(如果新运动向右)或向左(如果新运动向左)曲线到旧起点/终点。

我现在的问题是: 当向左移动时,SpritesNodes zRotation 正是我想要的。节点“看起来”到屏幕顶部。当向右移动时,SpriteNodes zRotation 不好 - 现在节点指向屏幕底部,这非常难看。

我尝试了不同的方法 - 我已经更改了用于 SpriteNode 的图像(那里的旋转)。我试图通过一个 Action 来改变 SpriteNode 的 zRotation。没有任何帮助。当然因为在我的 SKAction 中我说“orientToPath = true”。我现在问.. 是否有一种简单的方法可以让 SpriteNode 始终“查看”屏幕顶部,或者我是否必须在 update() 函数中手动更改 zRotation?

我问是因为 SKAction.follow (orientToPath) 中有这个属性,我问自己我是否可以在这种情况下使用它,但是否有办法根据我的需要稍微改变它。或者,如果这是不可能的 - 如果路径向左,为什么 orientToPath 与向右路径中的 orientToPath 不同?

这是我的问题的视频,也许比我的意思更容易理解。 https://youtu.be/vLH5nUlE91U

最佳答案

您可以尝试使用以下方法反转您的 UIBezierPath:

public func bezierPathByReversingPath() -> UIBezierPath

而不是反向 followPath 操作,并使用一个 bool 值来监视此操作,该 bool 值知道您是顺时针方向还是逆时针方向。

关于ios - Swift SpriteKit - SKAction 中的 zRotation 跟随路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38211524/

相关文章:

jquery - 允许用户使用 css 和 jquery 旋转图像

ios - SKPhysicsJoint 使旋转无法正常工作

ios - 您不能直接初始化此类 - 在 Storyboard/NIB 文件中,将类类型设置为子类之一

ios - SKNode接触其他SKNode识别

ios - JTAppleCalendar 代表不适用于 Swift

swift - 在 Swift 3 中获取 SKSpriteNode 的文件名

ios - SpriteNode 不可见

iphone - Cocos2d iPhone : Rotate Sprite using Accelerometer

ios - UISplitView Controller 修改和人机界面指南

iOS Swift 错误 - Objective C 方法...由方法提供