How can you add a gif as a imageNamed item in swift?

334 views Asked by At

I am attempting to swap a character in a game that does not move to a gif image that does move.

Currently, the code is

    let player = Witch(imageNamed: "player")

When I attempt to change the image to the gif by changing names, as expected, it does not work. How can I change, or add to, the code above to play the gif recurringly as the spriteNode's image that is the character? I want it to be able to be one or two lines, if possible. I have used

     var runAnimation = [SKTexture]()

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let playerNode = self.childNode(withName: "player")
    if(playerNode != nil){
        let animation = SKAction.animate(with: runAnimation, timePerFrame: 0.1)
        playerNode?.run(SKAction.repeatForever(animation))
    }
}

     let runAtlas = SKTextureAtlas(named: "run")
    for index in 1...runAtlas.textureNames.count{
        let textureName = String.init(format: "run%1d", index)
        runAnimation.append(SKTexture(imageNamed: textureName))
    }

but have received the error of

    Thread 1: Fatal error: Can't form Range with upperBound < lowerBound

If you can help, please let me know.

0

There are 0 answers