Pablo
Pablo

Reputation: 1332

SpriteNode animation is failing

I have been trying to have a sprite with more than one texture so that it looks as if it was moving. I was looking at other questions and nothing they have done has fixed my issue, this is my code:

    var textures = [SKTexture]()
    for x in 0...2 {
        let texture = SKTexture(imageNamed: "peng_" + String(x))
        textures.append(texture)
        print("pen_" + String(x))
    }
    let pen = SKSpriteNode(imageNamed: "peng_0")
    self.addChild(pen)
    let action = SKAction.animate(withNormalTextures: textures, timePerFrame: 0.1, resize: false, restore: true)
    pen.run(SKAction.repeatForever(action))

When I run the simulation, the sprite stays still with the "peng_0" texture, but I want it to iterate the other textures every 0.1s

Does anyone know what am I doing wrong, thanks.

Upvotes: 1

Views: 143

Answers (1)

user6193496
user6193496

Reputation:

Here you go set your code like this

    var array = ["R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8"]
    var textures:[SKTexture] = []
    for i in 0 ..< array.count{
        let texture: SKTexture = SKTexture(imageNamed: array[i])
        textures.insert(texture, at:i)
    }
   let pen = SKSpriteNode(imageNamed: "R1")
    self.addChild(pen)
    let animation = SKAction.animate(with: textures, timePerFrame: 8/60, resize: true , restore:false )
    pen.run =  SKAction.repeatForever(animation)

Upvotes: 1

Related Questions