Haunted Light – AnimatedSprite

Hello. Today I’ll be covering the AnimatedSprite class I’ve been creating for this project. It was a though one because of a lot of core changes in the program. Using SFML made it easier than I expected though because of its versatility. It also helped that I actually planned what the class was going to contain, and then build on from there. I even think that part saved me a lot of hassle since I first “sketched up” a first one, that build on having 2 separate classes, one containing the sprites that were having animations, and another handling the animation. But then I searched around to see other ways people have been doing AnimatedSprite classes and found a, what I though, much more resourceful way, simply moving the rectangle that shows a part of the texture by changing it’s position on the texture. The animation will have to use time, so framerate isn’t a factor in how fast they play.

Untitled-2It might seem like a lot of code, but I added some extra methods and constructors just for versatility

Now for the more meaty part of this post, the Methods.

getStartFrame(); – With this you can get which frame you set as your “idle”, or start-frame. If you haven’t set a custom start-frame for your animated sprite, it will give you the default frame, the first one.

setStartFrame(); – This is where you decide which frame is the “idle” frame. The default “idle”-frame is the first one. The method takes 4 inputs(width, height, frame and amountofFrames). It uses width and height to set the dimensions of the shown sprite. Frame, amountsofFrames and width is used to determine which frame to show on the row. The sprites are saved on 1-row spritesheets, so no need to use height for this part.

setFrame(); – This is very similar to setStartFrame(), in that it forces the animation to start on  another frame that the default one. It also shares the same types of input, but setFrame() does not need the width and height of the sprite. The difference is that this doesn’t determine which frame is the “idle”, so you can use this method when you want to show a specific frame of the animated sprite but not use it when the player is in the “idle” state.

playAnimation(); – This method simply moves the rectangle that contains what is shown of the texture. It takes 3 inputs(direction, frameDuration and play). The direction input is used to set the direction the sprite faces based on a grid(4-way). The frameDuration input tells the sprite when to change from one frame to another in seconds and play is a true/false that simply tells the sprite if it’s going to play its animation or stay on its current frame.

playAnimation(); – Just like setFrame() and setStartFrame(), this method is very similar to the previous playAnimation()-method, it has the same purpose but what is different is that this method “lacks” the direction input because it’s used when you want to animate a sprite that does not have it’s direction determined by a grid, like the player, who is always going to face the mouse cursor. This also has the play input that does the same thing in this method as in the previous one, to tell the sprite if it’s going to animate or not. The deltatime input however, is new for this one and isn’t used by the first one, simply because the first playAnimation method has its own clock that it uses to calculate the time between frames.

This was my take on creating an AnimatedSprite class. I’m sure there are better ways than this, and maybe I’ll remake the class later during the project, but right now it’s working the way we want it and we need it to work.

With regards

Calle Johansson, Gamedesign and programming