没有设置physicsBody

没有设置physicsBody

本文介绍了SKAction.moveByX 没有设置physicsBody.velocity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Swift 编写一个 SpriteKit 游戏,并且正在使用以下代码来移动我的精灵 - 但是,它似乎没有更新 velocity.dx:

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

我需要做些什么来让 moveByX 也更新速度吗?

Is there something I need to do to get the moveByX to also update the velocity?

推荐答案

动作和物理在 SpriteKit 中很大程度上是独立的机制.一般来说,如果你使用物理来移动一个物体,你不应该对它使用移动动作.如果您想移动已经使用物理的物体,请使用物理来移动它——施加力或冲量,或者直接设置它的速度.

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.

相反,当您使用动作来移动身体,或直接设置其position 时,这些更改不会通过物理引擎.如果你想在一个移动动作中找到一个节点的速度,你必须自己通过观察它在帧之间的位置变化来计算它.

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.

这篇关于SKAction.moveByX 没有设置physicsBody.velocity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 08:36