I'm writing a SpriteKit game using Swift, and am using the following code to move my sprite - however, it doesn't seem to be updating velocity.dx:
func walk(isRight: Bool, speed: Float) {
var newV = (isRight ? 1 : -1) * 20 * speed;
let moveAction = SKAction.moveByX(newV, y: 0, duration: 2.0)
self.runAction(SKAction.repeatActionForever(moveAction), withKey: "walking")
println("self.physicsBody.velocity.dx: \(self.physicsBody.velocity.dx)")
}
Here's what I get in the console:
self.physicsBody.velocity.dx: 0.0
Is there something I need to do to get the moveByX to also update the velocity?
Actions and physics are largely separate mechanisms in SpriteKit. Generally, if you're using physics to move a body, you shouldn't use move actions on it. If you want to move a body that's already using physics, use physics to move it -- apply a force or impulse, or set its velocity directly.
Conversely, when you use actions to move a body, or set its position
directly, those changes don't go through the physics engine. If you want to find the speed of a node during a move action, you'll have to calculate it yourself by observing the change in its position between frames.
来源:https://stackoverflow.com/questions/24522406/skaction-movebyx-not-setting-physicsbody-velocity