2012-04-08 64 views
0

我有这样的代码:动画在XNA

 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的第一个项目,因为它可以有一个愚蠢的错误:)

P.S.抱歉。有一个坐标为(xt,yt)的矩形,我需要动画将矩形移动到(xo,yo)

P.P.S.我补充完整的课程,因为我不明白我的错误。

+4

我们也不明白是什么错,因为你不解释应该发生什么以及发生了什么! – 2012-04-08 19:38:10

回答

4

您绘制的整个动画中的一帧..。你应该从OnDraw的呼叫传递与diferent X,Y ...

编辑:

1)你不需要计时器,游戏类中的绘制方法默认为每秒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;

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); 
} 
+0

所以,我试过了,但它不起作用。 :( – 2012-04-08 19:50:36

+0

没错,没有复制粘贴代码,你应该尝试找出Blau指出的错误。在你的pass函数中,你刚刚得到了一个while循环,它将你的代码锁定在。当你被困在一个循环中时,绘制函数不会被触发。 – 2012-04-08 19:55:16

+0

你的代码充满了设计错误...我已经编辑它来修复它们,现在它应该运行...;) – Blau 2012-04-08 20:38:55

1

如果你愿意用雪碧涡流,使您的动画(具体版本实际上),你可以使用下面的类。你必须使用Sprite Vortex 1.2.2,因为在更新的版本中XML格式被改变了。确保您添加属性的XML文件更改为“不编译”。

如果你需要一个工作的例子,我可以给你一个非常简单的例子。

p.s. Sprite Vortex应该使用其他程序来做同样的事情,但是1.2.2版本很麻烦,但不是太糟糕。

类是在这里:http://pastebin.com/sNSa7xgQ

使用雪碧涡(确保它是1.2.2)选择spritesheet并选择要进行动画处理的子图像。导出XML代码。

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