SKAction.moveByX not setting physicsBody.velocity

爷,独闯天下 提交于 2019-12-19 11:57:32

问题


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?


回答1:


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!