0

我有这个代码:

        public class Area
{
    Texture2D point;
    Rectangle rect;
    SpriteBatch _sB;
    GameTimer _gt;
    int xo, yo, xt, yt;
    //List<Card> _cards;

    public Area(Texture2D point, SpriteBatch sB)
    {
        this.point = point;
        this._sB = sB;
        xt = 660;
        yt = 180;
        xo = 260;
        yo = 90;
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        rect = new Rectangle(660, 180, 80, 120);
        spriteBatch.Draw(point, rect, Color.White);

        _gt = new GameTimer();
        _gt.UpdateInterval = TimeSpan.FromSeconds(0.1);
        _gt.Draw += OnDraw;
    }

    private void OnDraw(object sender, GameTimerEventArgs e)
    {
        this.pass(xo, yo);
        if (xo != xt) xo += (xt > xo) ? 10 : -10;
        if (yo != yt) yo += (yt > yo) ? 10 : -10;
    }

    public void pass(int x, int y)
    {
        rect = new Rectangle(x, y, 80, 120);
        _sB.Draw(point, rect, Color.Black);
    }
}

所以,我不明白出了什么问题。这是我使用 XNA 的第一个项目,因此可能会出现愚蠢的错误 :)

PS对不起。有一个坐标为(xt,yt)的矩形,我需要动画将矩形移动到(xo,yo)

PPS 我添加了带有更正的完整课程,因为我不明白我的错误。

4

2 回答 2

5

您在一帧中绘制整个动画..您应该使用与 OnDraw 不同的 x,y 调用 Pass...

编辑:

1)你不需要计时器,游戏类中的draw方法默认为每秒60帧......

2) Seconds 参数应计算为 (float) gametime.ElapsedTime.TotalSeconds;

float time;
int xt=660, yt=180;
int xo=260, yo=90;

public void Draw(SpriteBatch spriteBatch, float Seconds)
{
    rect = new Rectangle(660, 180, 80, 120);
    spriteBatch.Draw(point, rect, Color.White);

    this.pass(xo, yo, spriteBatch);
    time+= Seconds;
    if (time>0.3)
    {
        if (xo!=xt) xo+= (xt>xo) ? 10 : -10;
        if (yo!=yt) yo+= (yt>yo) ? 10 : -10;
        time = 0;
    }
}

public void pass(int x, int y, spritebatch sb)
{
    rect = new Rectangle(x, y, 80, 120);
    sb.Draw(point, rect, Color.Red);
}

正如你应该知道的那样,这个动画将以粗略模式移动......如果你想平滑移动你的精灵......你可以使用 Vector2 作为你的位置,使用 float 作为你的速度;

Vector2 Origin = new Vector2(260, 90);
Vector2 Target = new Vector2(660, 180);
Vector2 Forward = Vector2.Normalize(Target-Source);
float Speed = 100; // Pixels per second
float Duration = (Target - Origin).Length() / Speed;
float Time = 0;

public void Update(float ElapsedSecondsPerFrame)
{
   if (Time<Duration)
   {
      Time+=Duration;
      if (Time > Duration) {
          Time = Duration;
          Origin = Target;
      }
      else Origin += Forward * Speed * ElapsedSecondsPerFrame;
   } 
}

public void Draw()
{
    rect = new Rectangle((int) Origin.X, (int) Origin.Y, 80, 120);
    sb.Draw(point, rect, Color.Red);   
}
于 2012-04-08T19:43:48.730 回答
1

如果您愿意使用 Sprite Vortex 制作动画(实际上是特定版本),您可以使用以下类。您必须使用 Sprite Vortex 1.2.2,因为在较新的版本中 XML 格式发生了变化。确保您添加属性的 XML 文件更改为“不编译”。

如果您需要一个工作示例,我可以给您发送一个非常简单的示例。

ps Sprite Vortex 应该做与使用其他程序相同的事情,但是 v 1.2.2 有很多错误,但还不错。

课程在这里: http: //pastebin.com/sNSa7xgQ

使用 Sprite Vortex(确保它是 1.2.2)选择一个 spritesheet 并选择您想要动画的子图像。导出 XML 代码。

将该类添加到您的项目中,它会读取 XML 并添加自动为您创建动画的帧。

于 2012-11-17T05:40:06.933 回答