Welcome! In this episode, I am going to attempt to make the character animate while he is walking... also I am going to prevent him from walking off the edge of the form. First, let's do the animation: We are going to need a cycler for the animation. One of the tricky parts of animation is coordinating the animation cycler number with the display and the program execution. Here, it will be simple: while the button is pressed, we will update the cycler. When the buttons are released, the cycler will reset. Usually, the counter will be updated something like this: cycler = (cycler + 1) % numberofframes Since the numberofframes is four (if you look at the bitmap from the previous section, our Cycler will be like: cycler = (cycler + 1) % 4 And since % 4 is equivalent to & 3, we can do this. cycler = (cycler + 1) & 3 But, I'm going to pretend that I don't know if this is going to be the final picture. Instead, I'll make it a constant. So, here is what we add:
int animcycler; // This is the animation cycler that will count from 0 to 3 and back to 0. const int thewalkingframecount = 4; // How many frames are in each walking cycle. |
new Rectangle(thecharwidth * (chardirec << 2), 0, thecharwidth, thecharheight) becomes new Rectangle(thecharwidth * chardirec * thewalkingframecount, 0, thecharwidth, thecharheight) |
// Now update the animation cycler. animcycler = (animcycler + 1) % thewalkingframecount; |
if ( !goleft && !goright ) { // If neither key is pressed. animcycler = 0; // Reset the counter, and signal another redraw. updateartwork = true; } |
new Rectangle(thecharwidth * chardirec * thewalkingframecount, 0, thecharwidth, thecharheight) |
new Rectangle(thecharwidth * chardirec * thewalkingframecount + animcycler * thecharwidth, 0, thecharwidth, thecharheight) |
gfx.DrawImage(playerbmp, playerloc.X, playerloc.Y , new Rectangle(thecharwidth * chardirec * thewalkingframecount + animcycler * thecharwidth, 0, thecharwidth, thecharheight), GraphicsUnit.Pixel); |
if (playerloc.Left < 0) { // If the player goes off the edge of the form, then we will move him back to the edge. playerloc.Offset(-playerloc.Left, 0); } |
if (playerloc.Right > themapwidth) { // If player goes off right edge... move him back. playerloc.Offset(themapwidth - playerloc.Right, 0); } |
if (goleft) { playerloc.Offset(-themovespeed, 0); // Move player left. if (playerloc.Left < 0) { // If the player goes off the edge of the form, then we will move him back to the edge. playerloc.Offset(-playerloc.Left, 0); } updateartwork = true; chardirec = 0; // Look left. // Now update the animation cycler. animcycler = (animcycler + 1) % thewalkingframecount; } else if (goright) { playerloc.Offset(themovespeed, 0); // Move player right. if (playerloc.Right > themapwidth) { // If player goes off right edge... move him back. playerloc.Offset(themapwidth - playerloc.Right, 0); } updateartwork = true; chardirec = 1; // Look right. // Now update the animation cycler. animcycler = (animcycler + 1) % thewalkingframecount; } |