pablo pablo - 1 year ago 124
Swift Question

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))
print("pen_" + String(x))
let pen = SKSpriteNode(imageNamed: "peng_0")
let action = SKAction.animate(withNormalTextures: textures, timePerFrame: 0.1, resize: false, restore: true)

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.

Answer Source

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")
    let animation = SKAction.animate(with: textures, timePerFrame: 8/60, resize: true , restore:false ) =  SKAction.repeatForever(animation)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download