开发者

Sprites and C# animation

I found a class to make a gif file containing multiple frame animations run in front of a background image. This is my class:

using System;
using System.Drawing;
using Syste开发者_开发技巧m.Drawing.Imaging;
using System.Windows.Forms;

namespace AnimSprites {

     public class AnimSprite {
     private int frame, interval, width, height;
     private string imgFile;
     private Image img;
     private Timer frameTimer;

     public AnimSprite(string f_imgFile, int f_width) {
          frame = 0;
          width = f_width;
          imgFile = f_imgFile;

          img = new Bitmap(imgFile);
          height = img.Height;
     }

     public void Start(int f_interval) {
          interval = f_interval;

          frameTimer = new Timer();
          frameTimer.Interval = interval;
          frameTimer.Tick += new EventHandler(advanceFrame);
          frameTimer.Start();
     }

     public void Start() {
          Start(100);
     }

     public void Stop() {
          frameTimer.Stop();
          frameTimer.Dispose();
     }

     public Bitmap Paint(Graphics e) {
          Bitmap temp;
          Graphics tempGraphics;

          temp = new Bitmap(width, height, e);
          tempGraphics = Graphics.FromImage(temp);

          tempGraphics.DrawImageUnscaled(img, 0-(width*frame), 0);

          tempGraphics.Dispose();
          return(temp);
     }

     private void advanceFrame(Object sender, EventArgs e) {
          frame++;
          if ( frame >= img.Width/width )
               frame = 0;
          }
     }
}

How can I use this class to make my gif file (running_dog.gif) run over background.jpg from left to right?

This is the dog.gif file: dog.gif


The class you've included expects the animation frames to go from left to right rather than top to bottom as your .gif does.

You can alter it by changing the constructor to

public AnimSprite(string f_imgFile, int f_height) {
    frame = 0;
    height = f_height;
    imgFile = f_imgFile;

    img = new Bitmap(imgFile);
    width = img.Width;
}

and the advanceFrame method to

private void advanceFrame(Object sender, EventArgs e) {
    frame++;
    if ( frame >= img.Height/height )
       frame = 0;
    }
}

and your call to DrawImageUnscaled to

tempGraphics.DrawImageUnscaled(img, 0, 0-(height*frame));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜