Animating your sprites and entities - AnselmeClergeot/FisoEngine GitHub Wiki
After animating tiles, you may want to animate your entities or any other sprites. Let's learn how to do this !
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.
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.
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 exampleDeclare as many FrameSet as you need.
To set the current animation state of your animation (so the FrameSet), do :
anim.setCurrentFrameSet(walkingRight); //Or walkingLeft, anythingNow your Animation will play the wanted FrameSet.
The last thing to do is setting the status of your Animation, On or Off :
anim.setStatus(fe::Status::On);