cocoa-touch - 在不从 CGPath 开始的情况下使用 SKAction 跟随 CGPath

标签 cocoa-touch sprite-kit swift cgpath skaction

我有一个代表椭圆的 SKShape 节点。玩家被放置在基于椭圆 CGPath 的贝塞尔路径的当前点上:

enter image description here

我有两个玩家节点可以执行的操作。玩家可以顺时针或逆时针沿着路径行驶。

rotateCounterClockwiseAction = SKAction.followPath(counterClockwisePath.CGPath, asOffset: false, orientToPath: true, duration: 1)
rotateClockwiseAction = SKAction.followPath(clockwisePath.CGPath, asOffset: false, orientToPath: true, duration: 1)

当我开始以下操作之一时:

player.runAction(SKAction.repeatActionForever(rotateClockwiseAction), withKey: "ClockwiseRotation")

玩家沿着适当的路径和方向移动。当我停止其中一个 Action 时:

player.removeActionForKey("ClockwiseRotation")

玩家停在上次移动到的路径上。我希望能够从玩家的当前点开始任何一个 Action ,并遵循相同的路径,但是现在,如果我再次开始其中一个 Action (在 Action 已经开始和停止之后),玩家会跳到图中所示的相同起点,然后从那里沿着路径走。我怎样才能让玩家从它已经在的点开始跟随路径?

最佳答案

您可以使用 SKNode 的 speed 属性轻松实现这一点。

您可以通过调整速度值来暂停和恢复 Action ,而不是删除并重新创建 Action 。这将使您的贝塞尔曲线路径动画从当前位置恢复。

// Set the player node's speed to 0.0 causing all actions to be paused.
[playerNode setSpeed:0.0];

...

// Then when you're ready to resume set the player node's speed back to 1.0.
// This will cause the action to continue from its current position.
[playerNode setSpeed:1.0];

关于cocoa-touch - 在不从 CGPath 开始的情况下使用 SKAction 跟随 CGPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24234801/

相关文章:

iphone - 在 NSURLConnection 下载期间更新 iPhone UIProgressView

swift - 障碍物随机间隙不起作用 [SpriteKit] [Swift]

ios - 解析响应中的纯 json 字符串值 swift codable

ios - 如何使用 swift 在 Xcode 中制作自定义按钮?

ios - 检查 UIAlertController 中的字段验证

objective-c - UIButton 删除触摸覆盖边框

objective-c - 获取 NSDictionary 键的 NSArray,按 Value 排序,然后按 Key 排序

iphone - dispatch_async 没有被调用

ios - iPhone 游戏的 SpriteKit 关卡编辑器场景大小?

xcode - SpriteKit 的更新函数 : time vs. framerate