2011-11-14 34 views
2

我有問題。我已經嘗試瞭如何讓我的紋理移動,但我的解決方案很慢,它不工作。有誰知道如何使用C#XNAGamestudio進行texture2D移動。任何人請幫助我!如何使紋理移動

編輯:

GraphicsDeviceManager graphics; 
     SpriteBatch spriteBatch; 
     public Hashtable Objects = new Hashtable(); 
     public Hashtable XPOS = new Hashtable(); 
     public Hashtable YPOS = new Hashtable(); 
     public int NUM = 0; 
     public bool UP = true; 
     public bool DOWN = false; 
     public bool LEFT = false; 
     public bool RIGHT = false; 
    ...... 
protected override void Update(GameTime gameTime) 
     { 
      // Allows the game to exit 
      if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
       this.Exit(); 
      this.AddObject("Nameee", "C:\\Box.png"); 
      // TODO: Add your update logic here 
      if (UP) 
      { 
       if (NUM != 25) 
       { 
        AppendObject(new Vector2((float)this.XPOS["Nameee"], (float)this.XPOS["Nameee"] - NUM), "Nameee"); 
        NUM++; 
        Thread.Sleep(100); 
       } 
       else 
        UP = false; 
      } 
      base.Update(gameTime); 
     } 

     /// <summary> 
     /// This is called when the game should draw itself. 
     /// </summary> 
     /// <param name="gameTime">Provides a snapshot of timing values.</param> 
     protected override void Draw(GameTime gameTime) 
     { 
      //GraphicsDevice.Clear(Color.CornflowerBlue); 
      //spriteBatch.Begin(); 

      //spriteBatch.End(); 


      //base.Draw(gameTime); 
     } 
     public void AddObject(string TagName, string ObjectImage) 
     { 
      Texture2D fileTexture; 
      using (FileStream fileStream = new FileStream(@ObjectImage, FileMode.Open)) 
      { 
       fileTexture = Texture2D.FromStream(graphics.GraphicsDevice, fileStream); 
      } 
      if (!this.Objects.ContainsKey(TagName)) 
      { 
       this.Objects.Add(TagName, fileTexture); 
       this.XPOS.Add(TagName, 0.0F); 
       this.YPOS.Add(TagName, 50.0F); 
      } 
     } 
     public void AppendObject(Vector2 pos, string tagName) 
     { 
      spriteBatch.Begin(); 
      spriteBatch.Draw((Texture2D)this.Objects[tagName], new Vector2((float)this.XPOS[tagName], (float)this.YPOS[tagName]), Color.White); 
      spriteBatch.End(); 
     } 
+1

什麼你已經這樣做將是非常有幫助的一些代碼:) –

+0

我又增加了它。 – user35443

+0

看起來你正在爲每一幀創建一個新的紋理對象。那很不好。不要這樣做。 – thedaian

回答

3

你可以做類似這樣的東西,你應該使用內容管理器來加載資產。

public class Sprite 
{ 
    public Vector2 Position = Vector2.Zero; 
    public Texture2D Texture; 
    public float Scale = 1; 


    public void LoadAsset(Game game, string asset) 
    { 
     Texture = game.Content.Load<Texture2d>(asset); 
    } 

    public Draw(SpriteBatch batch) 
    { 
     batch.Draw(Texture, Position, null, Color.White, ...,... Scale,...); 
    } 
} 


//In your game: 

List<Sprite> Sprites = new List<Sprite>(); 

Initialize() 
{ 
    base.Initialize(); 
    Sprite myBox = new Box(); 
    myBox.LoadAsset("Box"); 
    Sprites.Add(myBox); 
} 


Update(GameTime gametime) 
{ 
    myBox.Position += Vector2.UnitX * Speed * (float) gametime.elapsed.TotalSeconds; 
} 


Draw() 
{ 
    batch.begin(); 
    foreach (Sprite sprite in Sprites) sprite.Draw(batch); 
    batch.end(); 
} 
+0

+1同樣的答案,但在代碼形式 –

+0

對不起,但我不能接受。我有這個錯誤:http://prntscr.com/41ufk 請幫我 – user35443

+0

請注意,這是僞代碼,它沒有測試...如果你在初始化方法中創建精靈,你必須調用base.Initialize( )之前。我編輯了代碼... :) – Blau

4

嗯,好技術,這看起來像它應該沿X軸移動紋理。

至於性能,您可能希望把這些東西考慮:

  1. 您正在呼籲每一個更新this.AddObject("Nameee", "C:\\Box.png");。這不是一件好事。在LoadContent()方法中調用一次。
  2. 而不是使用Thread.Sleep(100);,我會建議跟蹤GameTime已過去的時間。 (如果需要如何操作,請點擊註釋)
  3. 您正在爲紋理位置的每次通過創建一個新的vector2。雖然這可能沒有,因爲它只是創造10秒(感謝你Thread.Sleep(100);明顯的性能打,我會建議使用同一個。

在一個側面說明,我會建議完全重構的代碼創建一個包含Texture2D和Vector2的類,然後給它一個Update()方法和一個Draw(SpriteBatch sb)方法,並在那裏執行你的工作。

+0

+1太...對事實的很好的解釋,使得閱讀和代碼的表現非常糟糕 – Blau

2

Blau和justnS都給出了很好的答案,我還建議看看一些XNA教程來獲得更好的結果瞭解XNA框架及其應如何使用(即單獨Initialize(),LoadContent(),Update(),Draw()等方法的目的)。

嘗試這些對於初學者:

+0

+1,很好的建議(特別是Riemer的 - ***非常** *適合初學者) – jadarnel27