Last week we ran into an issue with our animation system. Whenever we played the projectile animation we were stuck in that animation as we had no way of transitioning out of it. In this tutorial, we’ll add the ability to set whether an animation is looped or only plays the once. We’ll look at animation transitions next week.
As well as passing all the usual frame data (textureID, texture position and size etc.) we also pass in a bool indicating whether the animation is looped.
You may be asking why we are setting the animations loop flag every time we add a frame rather than calling the mutator function. And that’s a very good question. I cannot think why I’ve written the code to set the animations looped flag every time we add a frame rather than just calling the mutator the once. This is a behind the scenes look at the process I follow for these tutorials but if you look in the GitHub repo you’ll notice that it contains the code up to tutorial 45 (and possibly even further by the time you read this) even though I have only completed the write-up for the first 30 tutorials. This happens for a number of reasons but it is usually because I want to finish the code for a topic (such as projectile attacks) before I start the write-up. This has the benefit that my programming flow is not constantly interrupted and I can normally write the code for a new feature in one sitting. Also by distancing myself from my code over time, when I come to do the write-up I an essentially performing my own code review and can hopefully spot most of the mistake I make (although if you notice any issues then please let me know in the comments, as with any large project there are bound to be some bugs that slip through). However, it does have one big downside which is when I come to do the write-up and I notice an error or oddity in my programming, which is exactly what has happened here, it is not as easy to fix as it would have been if I didn’t have the code for the next 15+ tutorials already written. Any change I make to the code base here needs to be replicated in each tutorials folder. One small problem with the replication and that tutorials code won’t compile. However, while these issues may happen every now and then don’t worry they will be fixed in time. In fact, tutorial 46 will contain the changes to how we set the looped flag but feel free to make your own changed if you can’t wait until then.
Anyway back to this weeks tutorial. With those changes made we can now run the game and when we press the ‘e’ key (the key we assigned to shoot a projectile in this tutorial) the animation will stop at the last frame.