2013-06-01 49 views
0

我正在使用XNA 4.0在c#中做一個小遊戲。我已經成功地讓我的精靈在所有方向上移動,並使它看起來像是實際上使用數組走路。到目前爲止,我只用「向上」鍵對此進行了測試。問題是,當我按下向上鍵時,精靈會移動,但是它會快速遍歷數組中的所有元素,以至於看起來他正在運行快速獲取他要去的大量空間。有什麼辦法可以減慢紋理彼此間的變化速度,比如暫停方法或其他方法。任何幫助都表示讚賞,謝謝。處理精靈時減緩數組中的紋理更改。

namespace RandomGame 
{ 

    public class Game1 : Microsoft.Xna.Framework.Game 
    { 
     GraphicsDeviceManager graphics; 
     SpriteBatch spriteBatch; 

     Color backColor = Color.FromNonPremultiplied(190, 230, 248, 250); 
     int i = 0; 
     public Game1() 
     { 
      graphics = new GraphicsDeviceManager(this); 
      graphics.IsFullScreen = false; 
      graphics.PreferredBackBufferHeight = 500; 
      graphics.PreferredBackBufferWidth = 800; 
      Content.RootDirectory = "Content"; 
     } 


     protected override void Initialize() 
     { 
      base.Initialize(); 

     } 

     Texture2D[] UpTextures = new Texture2D[6]; 
     Texture2D startTexture; 
     Texture2D leftTexture; 
     Texture2D rightTexture; 
     Vector2 position = new Vector2(380, 230); 

     protected override void LoadContent() 
     { 


      spriteBatch = new SpriteBatch(GraphicsDevice); 
      startTexture = Content.Load<Texture2D>("BlueLinkStart"); 
      leftTexture = Content.Load<Texture2D>("BlueLinkLeft"); 
      rightTexture = Content.Load<Texture2D>("BlueLinkRight"); 
      UpTextures[0] = Content.Load<Texture2D>("BlueLinkUp"); 
      UpTextures[1] = Content.Load<Texture2D>("BlueLinkUp2"); 
      UpTextures[2] = Content.Load<Texture2D>("BlueLinkUp3"); 
      UpTextures[3] = Content.Load<Texture2D>("BlueLinkUp4"); 
      UpTextures[4] = Content.Load<Texture2D>("BlueLinkUp5"); 
      UpTextures[5] = Content.Load<Texture2D>("BlueLinkUp6"); 
     } 

     protected override void UnloadContent() 
     { 

     } 

     protected override void Update(GameTime gameTime) 
     { 
      // Allows the game to exit 
      if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
       this.Exit(); 

      if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape)) 
      { 
       this.Exit(); 
      } 
      if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Left) && position.X > -3) 
      { 
       position.X -= 2; 

      } 
      if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Right) && position.X < 772) 
      { 
       position.X += 2; 
      } 
      if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Up) && position.Y > -3) 
      { 
       position.Y -= 2; 
      } 
      if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Down) && position.Y < 472) 
      { 
       position.Y += 2; 
      } 
      base.Update(gameTime); 
     } 

     protected override void Draw(GameTime gameTime) 
     { 
      GraphicsDevice.Clear(Color.FromNonPremultiplied(188, 231, 241, 255)); 
      if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Left)) 
      { 
       spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend); 
       spriteBatch.Draw(leftTexture, position, Color.White); 
       spriteBatch.End(); 
       base.Draw(gameTime); 
      } 
      else if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Right)) 
      { 
       spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend); 
       spriteBatch.Draw(rightTexture, position, Color.White); 
       spriteBatch.End(); 
       base.Draw(gameTime); 
      } 
      else if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Up)) 
      { 

       spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend); 
       spriteBatch.Draw(UpTextures[i], position, Color.White); 
       spriteBatch.End(); 
       i++; 
       if (i == 6) { i = 0; } 
       base.Draw(gameTime); 
      } 
      else 
      { 
       spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend); 
       spriteBatch.Draw(startTexture, position, Color.White); 
       spriteBatch.End(); 
       base.Draw(gameTime); 
      } 
     } 
    } 
} 

回答

1

是的,有。

首先,您需要一個保存動畫狀態的變量。你的i可能應該是這樣的。但是,您應該將其重命名爲animationState以反映其目的。此外,它更容易使其變爲floatdouble變量。

然後更新動畫是Update()方法的一項任務。你顯然依賴於60赫茲的更新頻率。對於小型遊戲來說這沒什麼問題,但是對於大型遊戲,您應該考慮可能的緩慢起伏。如果你有n精靈,你想改變精靈每m毫秒,那麼你更新animationState如下:

animationState += updateDuration * m; 
if(animationState >= n) animationState -= n; 

updateDuration是自上次更新的時間。所以對於60 Hz這是1000.0/60

然後你需要繪製在Draw()方法正確精靈:

spriteBatch.Draw(UpTextures[(int)animationState], position, Color.White); 
+0

慣於這項事業的索引越界異常? – ItalianStallion

+0

它不應該。 'animationState'總是'> = 0'和'