0
\$\begingroup\$

The following code is what I'm working on to animate characters in a SK scene class. I can run the code without building, no problem, but I can't build it and run it; I get one error message: Binary operator '+=' cannot be applied to operands of type '[(SKTexture)]' and '[SKTexture!}'. The code is below:

func initStoryBegin() {
    let CaitAtlas = SKTextureAtlas(named: "cait")
    let DrewAtlas = SKTextureAtlas(named: "drew")

    CwalkAnimation = self.animateSheet(CaitAtlas, walkAnimation: CwalkAnimation, formattedword: "cait%01d")

}
// ***This is where I am having trouble. In the walkAnimation += line.
func animateSheet(nameAtlas: SKTextureAtlas!, walkAnimation: [(SKTexture)], formattedword: String) -> [SKTexture] {
    for index in 1...nameAtlas.textureNames.count {
        let imgName = String(format: formattedword, index)
        walkAnimation += [nameAtlas.textureNamed(imgName)]
        println(walkAnimation)
        return walkAnimation
    }
}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    let charNode = self.childNodeWithName("charNode")

    if (charNode != nil) {
        let animation = SKAction.animateWithTextures(CwalkAnimation, timePerFrame: 0.2)

    charNode?.runAction(animation)

    }
}

I can't figure out why the code works when I run it, but it doesn't work when I try to build and run? Any help would be greatly appreciated.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Your walkAnimation array needs to be defined as inout in order to be mutated. Arrays in Swift are treated as Structs, which cannot be altered when passed by value, which is the default behavior. Using "inout walkAnimation: [(SKTexture)]" allows the array to be passed by reference, thus allowing a change such as your append.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks! I figured it out a while back, but had not come back to post the answer, but your explanation does the trick, so no need for me to reinvent the wheel. Thank you very much, I'd upvote it, but I don't have the reputation for that at the moment. \$\endgroup\$
    – Bennybear
    Commented Jun 3, 2015 at 20:34
  • \$\begingroup\$ You're quite welcome \$\endgroup\$
    – Attackfarm
    Commented Jun 4, 2015 at 6:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .