0
votes

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
Run sequence which will wait for 0.5 sec, and then change the texture. Do you need this to be synchronized with some event, eg. when node reaches certain point after some other action, or you just want to make a node to change its texture after certain amount of time no matter what ?Whirlwind
Basically I have a node that is moving from the left side of the screen to the right, and I have two textures that make it look like the image is walking. So as it is moving, I want to switch between the two textures (back and forth) and each texture lasts for a duration of 0.5 seconds.NickyNick321
You can use whatever you like in that case. Just run a sequence with a delay. In order to change a texture, you can follow Sangony's answer, or use setTexture method on desired node (you will do this inside a block) or don't use a block and use an action, like you are already doing. You can use pbodsk's way too, but because it is just two image swapping I suggest you to try for yourself both methods to see what suits you better. Animating with textures comes useful when you have a lot of textures to animate, but it requires an array filed with textures so it adds a little more "complexity" :)Whirlwind
Thanks for the response. I updated my question with my current code. Would you be able to tell me how to add the delay into this code so the "D2" and "DWalkRight" textures can switch back and forth while it is running the MoveByX action? I'm confused about where to put the sequences and delays.NickyNick321

2 Answers

0
votes

This is explained in the SKSpriteNode docs.

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

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