0
votes

I've been working on a Spritekit indie game project for about 5 months and I've come to a point where I have to make my hero fly, and use a texture animation for the wings to flap. When the "fly mode" starts and the hero jumps the wings have to show and start flapping. For this I made 3 distinct animations:

wigsOpenAnimation = SKAction.repeatAction(openAtlasAnimation, count: 1)
wingsFlapAnimation = SKAction.repeatActionForever(flapAtlasAnimation)
wingsCloseAnimaiton = SKAction.repeatAction(closeAtlasAnimation, count: 1)

and then a sequence to join the open animation with the first flap animation:

wingsSequence = SKAction.sequence([wingsOpenAnimation,wingsFlapAnimation])

However, when the hero lands has to close his wings, and this is the piece of code I used to do so:

        if (self.onGround == true) || (self.flyMode == false) {
        if String(wings.texture).rangeOfString("Flap8") != nil {


            wings.runAction(SKAction.waitForDuration(wingsFrequency)) {
                self.wings.removeActionForKey("wingsSequence")
                self.wings.runAction(self.wingsCloseAnimaiton)


            }

        }
    }

I put this piece of code in the update function, and "Flap8" is the last flap frame texture. The problem is that with this code, immediately before starting the close animation, Flap1 shows again, so it's like the Flap sequence starts again. This is bad because the texture are made so that the close animation has to come immediately after the last flap frame, and having another flap frame in between doesn't look good. Any possible solutions are welcome. I hope I was clear enough, and thanks.

1
maybe manually set the texture to be whichever one you want it to be before you run your wingCloseAnimation?hamobi
Yes could have been a good idea, but I solved it in another way. When telling it how long to wait I previously put "wingsFreqeuncy", and now I subtracted the amount of time that passes between each frame update (so for example if you have 60 FPS it would be 1/60 sec) so that it doesn't run the extra frame. Thank you for the advice anyway!Edward Danescu

1 Answers

0
votes

Swift 4.0 Code

Here is a strategy that I used:

    Sprite.run(SKAction.repeatForever(SKAction.animate(with: Sprite1TextureArray, timePerFrame: 0.15)))

        DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(900) /**Amount of frames * timePerFrame*/, execute: {
                  //Your second animation  
                  self.Sprite.run(SKAction.repeatForever(SKAction.animate(with: self.Sprite2TextureArray, timePerFrame: 0.15)))
        })

Basically what this does is it runs your animation for as long as you want, and then replaces the animation with a new one after the number of seconds or milliseconds that you specify.

Hint: 0.1 Seconds = 100 milliseconds