0

I created a sprite with an animated texture atlas. I then want that animation to change based on the direction the sprite is heading. I try to do so with "self.player!.texture = firstFrametexture(ofatlas)"

Here is where I build the player (put inside didMove but the animation starts without having to move?)

func buildPlayer() {
  let playerAnimatedAtlas = SKTextureAtlas(named: "animation")

  var walkFrames: [SKTexture] = []

  let numImages = playerAnimatedAtlas.textureNames.count
  for i in 1...numImages {
    let playerTextureName = "player\(i)"

    walkFrames.append(playerAnimatedAtlas.textureNamed(playerTextureName))

  }
  walkingPlayer = walkFrames

    let firstFrameTexture = walkingPlayer[0]
    player! = SKSpriteNode(texture: firstFrameTexture)

    player!.position = CGPoint(x: frame.midX, y: frame.midY)
    addChild(player!)
}

Here is where I animate the player

func animatePlayer() {
  player!.run(SKAction.repeatForever(
    SKAction.animate(with: walkingPlayer,
                     timePerFrame: 0.5,
                     resize: false,
                     restore: true)),
    withKey:"walkingInPlacePlayer")
}

Here is where I try to change the animation. Everything works, the if statement are correctly executed. Just nothing changes

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

        let lplayerAnimatedAtlas = SKTextureAtlas(named: "animationleft")
             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(lplayerAnimatedAtlas.textureNamed(playerLeftTextureName))
             }



             walkingPlayer = walkFrames
             lwalkingPlayer = lwalkFrames


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



        if location.x > player!.position.x {
            self.player!.texture = firstFrameTexture
            print("rightsuccess")
        } else if location.x < player!.position.x {
            self.player!.texture = leftFrameTexture
            print("leftsuccess")
        } 

    }
}
Mugs
  • 339
  • 3
  • 14
  • 1
    I don't see anything within `touchesBegan` where you're changing the animation. (Or for that matter where you're calling `animatePlayer` at all.) As an overall organization, you should have two animations (for walk left and right), and maybe a third if you have a standing animation too. Whenever you want the animation to change in response to controls, call `removeAllActions` on the player to stop the previous animation and then call `run` with the new desired animation. – bg2b Jun 07 '20 at 19:50
  • oh I thought the animation just looped through images. So if i changed the atlas / texture, it would just loop through a different set of images. I'm going to have a bunch of animation, im doing a top down rpg like neverwinter nights. For now im just going to do the 4 directions, then ill prob pay someone to animate more so it looks more natural. I'm not sure how to do what you said above in code, any chance you have a resource I can look at? im confused at how exactly I should add the animation and change the texture bc from what I know they are very separate functions – Mugs Jun 07 '20 at 20:14
  • This post is doing something similar; on changing directions, they want to change the animation that's shown: https://stackoverflow.com/questions/54227703/spritekit-animation-not-changing – bg2b Jun 08 '20 at 01:35

0 Answers0