SpriteKit texture duration

285 views Asked by At

I am making a game and I have a node that switches back and forth between 2 textures. I was wondering how I can add a 0.5 second duration for each texture before it switches back to the previous one. Right now when I run the game the textures switch instantly, so I would like to delay it 0.5 seconds.

My current code:

func move(){

    let recursive = SKAction.sequence([

        SKAction.setTexture(SKTexture(imageNamed: "D2")),
        SKAction.setTexture(SKTexture(imageNamed: "DWalkRight")),
        SKAction.moveByX(frame.size.width/2.8, y: 0, duration: NSTimeInterval(randomNumber())),
        SKAction.setTexture(SKTexture(imageNamed: "D1")),
        SKAction.setTexture(SKTexture(imageNamed: "DWalkLeft")),
        SKAction.moveByX(-frame.size.width/2.8, y: 0, duration: NSTimeInterval(randomNumber())),
        SKAction.runBlock({self.move()})])

        Drake1.runAction(recursive, withKey: "move")
}
2

There are 2 answers

0
pbodsk On

You could use a SKTextureAtlas containing your images.

This atlas you could then load into an array which you could use with SKAction.animateWithTextures:timePerFrame:

For a good introduction to SKTextureAtlas you could take a look at this tutorial.

Hope this is useful

0
sangony On

This is explained in the SKSpriteNode docs.

myNode.texture = SKTexture(imageNamed: "imageName")