How to change the texture atlas of a node in an SKAction sequence?

166 views Asked by At

I'm making a game with SpriteKit where I have a node that is repeatedly moving back and forth from the left to the right side of the screen using this SKAction sequence:

func move(){
    let recursive = SKAction.sequence([
        SKAction.moveByX(frame.size.width/2.8, y: 0, duration: NSTimeInterval(4),
        SKAction.moveByX(-frame.size.width/2.8, y: 0, duration: NSTimeInterval(4),
        SKAction.runBlock({self.move()})])
        doc.runAction(recursive, withKey: "move")
} 

The node is an SKTexture, and I'm using an atlas called WalkRight. What I am trying to do, is when the SKAction.moveByX(-frame.size.width/2.8, y: 0, duration: NSTimeInterval(4) is run, I want to change the texture of the node to a different atlas that I have called WalkLeft. I can't figure out how to run an action in my sequence that will let me change textures of the node.

The code I have for adding the texture:

 //In class
var docWalkingFrames: [SKTexture]!

 // general runAction method to make doc walk.
func walkingDoc() {
    doc.runAction(SKAction.repeatActionForever(SKAction.animateWithTextures(docWalkingFrames,timePerFrame: 0.3,resize: false,restore: true)),withKey:"walkingInPlaceDoc")
}

//In didMoveToView
  //doc animation walk right
    let docAnimatedAtlas = SKTextureAtlas(named: "WalkRight")
    var walkFrames = [SKTexture]()
    let numImages = docAnimatedAtlas.textureNames.count
    for var i=1; i<=numImages; i++ {
        let docTextureName = "docRight\(i)"
        walkFrames.append(docAnimatedAtlas.textureNamed(docTextureName))
    }

    docWalkingFrames = walkFrames

    let firstFrame = docWalkingFrames[0]
    doc = SKSpriteNode(texture: firstFrame)
    doc.position = CGPoint(x:self.frame.size.width/3.1, y:self.frame.size.height/2)
    doc.size = CGSize(width: 220, height: 470)
    doc.zPosition = 2
    addChild(doc)

walkingDoc()
0

There are 0 answers