Animating your sprites and entities - AnselmeClergeot/FisoEngine GitHub Wiki

Let's learn how to animate a sprite !

After animating tiles, you may want to animate your entities or any other sprites. Let's learn how to do this !

Introduction

To animate a tile, you will need a spritesheet. A spritesheet is a frame board that looks like this :

I advise you to keep this spritesheet layout : one animation frame per line (walk left/walk right/etc.). It will be more convenient. When you have find a simple spritesheet (or the one above) you can go on.

1) Declaring our animation

We will use our spritesheet as "spritesheet.png". Let's start by declaring our Animation :

Animation anim("spritesheet.png");
//Or
Animation anim;
anim.setSpriteSheet("spritesheet.png");

Our animation is "ready". Let's go on.

2) Declaring one or multiple FrameSet

If you want to use an animation, you will also have to use the FrameSet class.

First, what is a FrameSet ? A FrameSet is the class that represents one animation state : this last may be "walking", "walking-left", and other things like that. So one FrameSet per animation state. It is called "FrameSet" because it represents a group of frames in the spritesheet that will be played during animation.

Using FrameSet means using "frames". A frame is a part of an animation state. So a frame has 4 informations, in order :

  • The x coordinate of the frame in the spritesheet,
  • The y coordinate of the frame in the spritesheet,
  • The width of this frame,
  • The height of this frame

You will have to add different frames to your FrameSet in the order they should play. Let's beggin by declaring our "walkingRight" FrameSet :

FrameSet walkRight;

walkRight.addFrame(35, 130, 24, 48);
walkRight.addFrame(68, 131, 24, 47);
walkRight.addFrame(99, 130, 24, 48);
walkRight.addFrame(132, 131, 24, 47);

And the animation state corresponding to "walking to the right" is ready.

Note that you can reset the frames of your FrameSet by doing :

walkRight.resetFrames();

Now your FrameSet does not contain frames anymore.

The last thing you would want to do is setting the speed of this FrameSet. Do :

walkRight.setSpeed(66); //For example

Declare as many FrameSet as you need.

3) Setting or changing the current Animation's FrameSet

To set the current animation state of your animation (so the FrameSet), do :

anim.setCurrentFrameSet(walkingRight); //Or walkingLeft, anything

Now your Animation will play the wanted FrameSet.

Setting the status of your Animation

The last thing to do is setting the status of your Animation, On or Off :

anim.setStatus(fe::Status::On);

Well done !

⚠️ **GitHub.com Fallback** ⚠️