Why doesn't my sprite animate when I call player.dodgeLeft()?

Viewed 23

I'm making my first spritekit game. Why doesn't my sprite animate when I call player.dodgeLeft() in my GameScene file? dodgeLeft() is defined in Player as well as the texture arrays. The texture arrays are populated through initializeAnimations(), and I checked that there are three values in each at runtime. Thanks!

Gamescene.swift

import SpriteKit
import GameplayKit

class GameScene: SKScene
{
    let player = Player(restSprite: SKSpriteNode(imageNamed: "Rest"))
    
    override func didMove(to view: SKView)
    {
        backgroundColor = SKColor.white
        self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
        player.currentSprite.setScale(0.25)
        addChild(player.currentSprite)
        player.initializeAnimations()
        player.dodgeLeft()
    }
    
}

Player.swift

import SpriteKit

class Player {
    let maxHealth = 100
    var currentSprite: SKSpriteNode
    var health = 100
    var stamina = 100
    let movementSpeed = 0.1
    var dodgeLeftTextures: [SKTexture] = []
    var dodgeRightTextures: [SKTexture] = []

    init(restSprite: SKSpriteNode) {
        self.currentSprite = restSprite
    }

    func initializeAnimations() {
        for i in 1.. .3 {
            dodgeLeftTextures.append(SKTexture(imageNamed: "Dodge Left \(i)"))
        }
        for i in 1.. .3 {
            dodgeRightTextures.append(SKTexture(imageNamed: "Dodge Right \(i)"))
        }
    }

    func dodgeLeft() {
        let dodgeLeftAnimation = SKAction.animate(withNormalTextures: dodgeLeftTextures, timePerFrame: movementSpeed, resize: false, restore: true)
        currentSprite.run(SKAction.repeatForever(dodgeLeftAnimation))
    }

    func dodgeRight() {
        let dodgeRightAnimation = SKAction.animate(withNormalTextures: dodgeRightTextures, timePerFrame: movementSpeed, resize: false, restore: true)
        currentSprite.run(dodgeRightAnimation)
    }

}
0 Answers
Related