1
votes

Value of type 'SKSpriteNode' has no member 'velocity'

I want to change the animation based on the velocity of my SKSprite node, but I'm getting the error shown above and I'm not sure why

override func touchesBegan(_ touches: Set, with event: UIEvent?) { super.touchesBegan(touches, with: event)

    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)
        }

    }
}
1

1 Answers

1
votes

Try player!.physicsBody.velocity

(I know this isn't your question, but eventually try to not need the ! everywhere -- use if let and guard let)