Traxido
Traxido

Reputation: 1

Problem with Convenience Inits for SKSpriteNodes and assigning Variable values

I've been programming for about 2 years, and as such can usually debug my way out of a certain problem but I can't seem to figure this one out.

I'm currently attempting to make a procedurally generated character generation method for one of my newer games still in the making. One of the many properties a character can have is clothing. As such, each clothing item needs to animate when the player runs, or attacks etc. I figured I could create multiple layers (SKSprites) on the character and animate each one separately for each layer of clothing.

Currently, I have a Convenience Init for each clothing item that takes the original image name, and assigns it to a few variables. One of these is an array of SKTextures: [SKTextures] which is the container for my running animations. Essentially, from the original image name, I use string interpolation to form a list of running layers for that certain clothing layer.

Here is my Clothing Class for reference:

import UIKit
import SpriteKit

class Clothing: SKSpriteNode {

    var staticImage = SKTexture()

    var runningAnimation : [SKTexture] = []
    var attackingAnimation : [SKTexture] = []
    var idleAnimation : [SKTexture] = []
    var deathAnimation : [SKTexture] = []

    convenience init(image: String) {
        self.init()
        staticImage = SKTexture.init(imageNamed: image)


        for i in 0...3 {
            runningAnimation.append(SKTexture.init(imageNamed: "\(image)Running\(i)"))
            //            attackingAnimation.append(SKTexture.init(imageNamed: "\(image)attack\(i)"))
            //            idleAnimation.append(SKTexture.init(imageNamed: "\(image)idle\(i)"))
            //            deathAnimation.append(SKTexture.init(imageNamed: "\(image)dying\(i)"))
        }

        self.texture = staticImage
    }

    func animate(animation: String) {

        var animate = SKAction()

        if animation == "run" {
            print(staticImage)
            animate = SKAction.animate(with: runningAnimation, timePerFrame: 0.15)
        }
    }
}

However my problem comes from the actual calling of animate(animation: "run") from the character class.

I'm met with a response in the debugger that's as follows:

2019-12-23 02:51:43.342512-0500 No Man's Land[6390:1805219] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'SKAction animateWithTextures: textures array cannot be empty'

I have tried adding print functions just just under my for i in loop to make sure that it's actually adding the textures and it is. If you look above, I also printed the staticImage when calling the animation function to see if something is happening when this function is called and I got an odd response in the Debugger:

<SKTexture> 'caucasianMaleHead' (160 x 160)
<SKTexture> '(null)' (0 x 0)

There is only one print function, and it seems as if it removes the value of the SKTexture that was assigned in the original init.

I've come to the conclusion that it must be something that happens when the animate function is called, but I'm not sure what, nor why it would remove values of previously set variables. I could be wrong though.

Any help or tips would be greatly appreciated, Thank you!

Upvotes: 0

Views: 56

Answers (0)

Related Questions