If you enjoy this tutorial, please consider supporting it by purchasing a book through one of the links on my site, such as this one ->
This is part of an ongoing series where we write a complete 2D game engine in C++ and SFML. A new tutorial is released every Monday. You can find the complete list of tutorials here and download the source code from the projects GitHub page.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.Unsurprisingly we start this week by making changes to the Animation class. We’ll add a bool to define whether an Animation is looped and a getter and setter to provide access to the bool.We set the default looped state to true in the constructor. With our flag created the first change we’ll make is to how we increment frames. Currently, the animation will increment the frame when the current one expires regardless of whether it is the last frame or first. Now we have the looped flag we need to add an additional check. We check if the frame is looped or the current frame index is less than the total frames -1 as we are only interested in the looped flag state when it is the last frame in the animation.Next, we’ll change the AddFrame function so we also pass in whether the animation is looped. 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.As we’ve changed the frame insertion function we will also need to update any code calling the function. I’ve added a flag for each animation that indicates whether the animation is looped or not. Idle and projectile animations are set not to loop and the walking animation is set to loop. We would normally expect the idle animation to loop but our idle ‘animation’ consists of only the one frame so no loop is required.
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.
Player stuck in last animation frame.We need some way of transitioning to a different animation when we no longer want to shoot projectiles, which is something we will work on next week.As always, if you have any suggestions for what you would like covered or are having any trouble implementing a feature, then let me know in the comments and I’ll get back to you as soon as I can. Thank you for reading 🙂