Difference between revisions of "Animation"
Line 1: | Line 1: | ||
− | + | This will show you how to animate a character. The idea is to use a texture which is actually a strip of frames, then knowing the width and height of each frame should allow you to only display the part of the texture which represents the current frame. | |
− | |||
− | + | ==Changing the Player Class== | |
− | + | Firstly, change the name of the Texture2D to something more appropriate eg PlayerAnimation: | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<syntaxhighlight lang=csharp> | <syntaxhighlight lang=csharp> | ||
− | + | public Texture2D PlayerAnimation; | |
− | |||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | We now need to add the following variables to the player to control the frame: | |
<syntaxhighlight lang=csharp> | <syntaxhighlight lang=csharp> | ||
− | // | + | // Get the width of the player ship |
− | + | // The time since we last updated the frame | |
− | + | int elapsedTime; | |
− | // The time since we last updated the frame | ||
− | int elapsedTime | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | // The | + | // The number of frames that the animation contains |
− | + | int frameCount; | |
− | // The | + | // The index of the current frame we are displaying |
− | + | int currentFrame; | |
− | // | + | // The color of the frame we will be displaying |
− | + | Color color; | |
− | // | + | // The area of the image strip we want to display |
− | + | Rectangle sourceRect = new Rectangle(); | |
− | // The | + | // The area where we want to display the image strip in the game |
− | + | Rectangle destinationRect = new Rectangle(); | |
− | // | + | // Width of a given frame |
− | public | + | public int FrameWidth; |
− | // | + | // Height of a given frame |
− | public | + | public int FrameHeight; |
− | |||
− | |||
+ | ==Change the Initialize method== | ||
<syntaxhighlight lang=csharp> | <syntaxhighlight lang=csharp> | ||
− | public void Initialize(Texture2D | + | public void Initialize(Texture2D animation, Vector2 position) |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | { | |
− | + | PlayerAnimation = animation; | |
− | |||
− | + | // Set the starting position of the player around the middle of the screen and to the back | |
− | + | Position = position; | |
− | |||
− | + | // Set the player to be active | |
− | + | Active = true; | |
− | |||
− | |||
− | + | // Set the player health | |
+ | Health = 100; | ||
+ | |||
+ | this.color = Color.White; | ||
+ | this.FrameWidth = 115; | ||
+ | this.FrameHeight = 69; | ||
+ | this.frameCount = 8; | ||
− | + | // Set the time to zero | |
− | + | elapsedTime = 0; | |
− | + | currentFrame = 0; | |
− | + | } | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | } | ||
− | |||
− | |||
− | |||
<syntaxhighlight lang=csharp> | <syntaxhighlight lang=csharp> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |
Revision as of 14:02, 25 March 2017
This will show you how to animate a character. The idea is to use a texture which is actually a strip of frames, then knowing the width and height of each frame should allow you to only display the part of the texture which represents the current frame.
Changing the Player Class
Firstly, change the name of the Texture2D to something more appropriate eg PlayerAnimation:
public Texture2D PlayerAnimation;
We now need to add the following variables to the player to control the frame:
<syntaxhighlight lang=csharp> // Get the width of the player ship // The time since we last updated the frame int elapsedTime;
// The number of frames that the animation contains int frameCount;
// The index of the current frame we are displaying int currentFrame;
// The color of the frame we will be displaying Color color;
// The area of the image strip we want to display Rectangle sourceRect = new Rectangle();
// The area where we want to display the image strip in the game Rectangle destinationRect = new Rectangle();
// Width of a given frame public int FrameWidth;
// Height of a given frame public int FrameHeight;
Change the Initialize method
<syntaxhighlight lang=csharp> public void Initialize(Texture2D animation, Vector2 position)
{ PlayerAnimation = animation;
// Set the starting position of the player around the middle of the screen and to the back Position = position;
// Set the player to be active Active = true;
// Set the player health Health = 100;
this.color = Color.White; this.FrameWidth = 115; this.FrameHeight = 69; this.frameCount = 8;
// Set the time to zero elapsedTime = 0; currentFrame = 0; }
<syntaxhighlight lang=csharp>