2012-04-08 62 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並添加自動爲您創建動畫幀。