根据速度更改swift SKSprite 节点动画

Change swift SKSprite node animation based on velocity

类型 'SKSpriteNode' 的值没有成员 'velocity'

我想根据我的 SKSprite 节点的速度更改动画,但我收到上面显示的错误,我不确定为什么

override func touchesBegan(_ touches: Set, with event: UIEvent?) { super.touchesBegan(触摸,有:事件)

    if let location = touches.first?.location(in: self) {
    let horizontalAction = SKAction.move(to: location, duration: 1.0)
        horizontalAction.timingMode = SKActionTimingMode.easeOut
      player?.run(horizontalAction)

        let playerAnimatedAtlas = SKTextureAtlas(named: "animation")
             var walkFrames: [SKTexture] = []
               var lwalkFrames: [SKTexture] = []


             let numImages = playerAnimatedAtlas.textureNames.count
             for i in 1...numImages {
               let playerTextureName = "player\(i)"
               let playerLeftTextureName = "lplayer\(i)"
               walkFrames.append(playerAnimatedAtlas.textureNamed(playerTextureName))
               lwalkFrames.append(playerAnimatedAtlas.textureNamed(playerLeftTextureName))
             }
             walkingPlayer = walkFrames
             lwalkingPlayer = lwalkFrames

               let leftFrameTexture = lwalkingPlayer[0]
               let firstFrameTexture = walkingPlayer[0]

        player!.physicsBody?.isDynamic = true

        player!.physicsBody = SKPhysicsBody(texture: firstFrameTexture,
                   size: player!.texture!.size())

        if player!.velocity.dx < 0 {
 //ERROR: Value of type 'SKSpriteNode' has no member 'velocity' ****
            player! = SKSpriteNode(texture: leftFrameTexture)
        }

        else if player!.velocity.dx > 0 {
//ERROR: Value of type 'SKSpriteNode' has no member 'velocity' ****
            player! = SKSpriteNode(texture: firstFrameTexture)
        }

    }
}

尝试player!.physicsBody.velocity

(我知道这不是你的问题,但最终尽量不要到处都需要 !——使用 if letguard let