Implementation of Animation Code

This week I have mostly been implementing animation for several sprites and special effects, using an animation code I made beforehand. I have previously used this code only to switch singular frames on sprites, but, as I am aware of now, it is a bit more difficult to cycle through animation frames using this code unless they are in a specific order.

Overall, the function is fairly simple and looks something like this:

inline void Animation(sf::Sprite* p_sprite, sf::Vector2i p_vectorI, int p_xMin, int p_xMax, int p_yMin, int p_yMax, int p_width, int p_height)

  • The sf:.Sprite* paramenter uses the sprites setTextureRect function  to switch frame on the spritesheet.
  • sf::Vector2i p_vectorI is basically the frames of the animation (if each frame is the same size). As it holds both x and y values it can scroll through frames by incrementing them on the x-axis (row), or the y-axis(column) for larger spritesheets.
  • The six other integers in the parameter determines the minimun and maximum frames as well as each frames width and height.

The function muliplicates the values of sf::Vector2i p_vectorI in x-axis and y-axis with the width and height of the frame, respectively, then checks if they are above or below the maximum allowed size, then sets the TextureRect of the sprite to the mulitplicated value.

It is of course arguable if the min/max values of frames’ size really are  necessary, as the code is relatively simple and requires a lot of additional parameters.

For example; as I implemented the animation for the player’s sprite, I bumped into the first problem with the function: The sprite needed to cycle between three frames if it turned and cycle through roughly four frames when it fired. The frames for turning were seperated from their respective frames for firing, meaning that to animate the firing correctly, my function would first have to ”jump” to the second frame of each firing animation, then cycle through them, ending by jumping back to the respective turning frame.

As this was a bit problematic to accomplish, I decided to switch the positions of the frames of the spritesheet so that each five frame animation were on a row by each other.

 

Baked potato explosion sprite sheet.pngPlayerPlane Death Animation Spritesheet.png

PlayerPlane Spritesheet
Spritesheets ready for animation! The top two are fairly simple, designed to cycle through all frames in succession. The one that’s third from the top, however, requires the animation to cyclethrough five frames and repeat, depending on the turning direction of the sprite and if it fires.

 

PlayerPlane Spritesheet
Spot the difference with the above? The turning sprites are seperate from their respective fire animation frames, making the usage of my function a bit more difficult.

 

 

 

About Erik Nord

2015 Programming