問題描述
SKCameraNode 跟不上移動節點 (SKCameraNode doesn't keep up with moving node)
我目前正在嘗試製作一個“launch(er) game”,即使用 SpriteKit 的 Toss the turle、Learn to fly 和 Burrito Bison 風格的遊戲。我已經使用 SpriteKit 的內置物理原理啟動了啟動工作,但是當我嘗試使用 SKCameraNode 跟隨主角時,似乎總是落後一個(或更多)一步,使主角“搖晃” " 在高速下。
我嘗試使用 SKAction 和 .position‑property 設置相機的位置,結果相同。
我猜這是因為物理更新的速度比實際的 update() 更快,我嘗試搜索有關此的信息,但找到了 zilch。
“啟動”主角的功能:
p>參考解法
方法 1:
By manually setting the camera position in update
, you're delaying the camera movement by at least one frame — physics runs after update
, so your camera move happens on the frame after your character moves.
When you use a move action instead of directly setting the position, and giving that action a nonzero duration, you're delaying the camera move even more. (The dt
in your code is the time between this frame and the last one before it, and you're applying that time to a future movement.) Because the character is still moving while your action runs, the camera will never catch up — you're always moving the camera to where the character was.
Setting the camera position at all is just making extra work for yourself, though. Use SKConstraint
instead, and SpriteKit itself will make sure that the camera position sticks to the character — it solves constraints after physics, but on the same frame.