2014-09-26 70 views
0

我正在嘗試製作一個角色並讓他左右走動。雖然我學會了如何製作基本的動畫,但我無法弄清楚如何在它們之間切換。如何在C#XNA程序中的動畫之間切換?

當角色向左走時,我希望他從「空閒」(animatedSprite)動畫切換到「左行走」(animatedSprite2)動畫。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Audio; 
using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.GamerServices; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 
using Microsoft.Xna.Framework.Media; 

namespace WalkingAnimation 
{ 
    public class Game1 : Microsoft.Xna.Framework.Game 
    { 
     GraphicsDeviceManager graphics; 
     SpriteBatch spriteBatch; 
     private AnimatedSprite animatedSprite, animatedSprite2; 
     private Vector2 position = new Vector2(350f, 200f); 
     private KeyboardState keyState; 

     public Game1() 
     { 
      graphics = new GraphicsDeviceManager(this); 
      Content.RootDirectory = "Content"; 
     } 

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

     protected override void LoadContent() 
     { 
      spriteBatch = new SpriteBatch(GraphicsDevice); 
      Texture2D texture = Content.Load<Texture2D>("Idle"); 
      Texture2D texture2 = Content.Load<Texture2D>("Run"); 
      animatedSprite = new AnimatedSprite(texture, 1, 11); 
      animatedSprite2 = new AnimatedSprite(texture2, 1, 10); 
     } 

     protected override void UnloadContent() 
     { 
     } 

     protected override void Update(GameTime gameTime) 
     { 
      keyState = Keyboard.GetState(); 

      if (keyState.IsKeyDown(Keys.Q)) 
      { 
       position.X -= 3; 
      } 
      if (keyState.IsKeyDown(Keys.P)) 
      { 
       position.X += 3; 
      } 

      base.Update(gameTime); 
      animatedSprite.Update(); 
      animatedSprite2.Update(); 
     } 

     protected override void Draw(GameTime gameTime) 
     { 
      GraphicsDevice.Clear(Color.CornflowerBlue); 

      base.Draw(gameTime); 
      animatedSprite.Draw(spriteBatch, position); 
     } 
    } 
} 

回答

0

像你這麼清楚你的問題表示,所有你需要做的是存儲Boolean描述字符是否空閒或沒有:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Audio; 
using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.GamerServices; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 
using Microsoft.Xna.Framework.Media; 

namespace WalkingAnimation 
{ 
    public class Game1 : Microsoft.Xna.Framework.Game 
    { 
     GraphicsDeviceManager graphics; 
     SpriteBatch spriteBatch; 
     private AnimatedSprite animatedSprite, animatedSprite2; 
     private Vector2 position = new Vector2(350f, 200f); 
     private KeyboardState keyState; 
     private bool idle; 

     public Game1() 
     { 
      graphics = new GraphicsDeviceManager(this); 
      Content.RootDirectory = "Content"; 
     } 

     protected override void Initialize() 
     { 
      base.Initialize(); 
      // Start of as idle 
      idle = true; 
     } 

     protected override void LoadContent() 
     { 
      spriteBatch = new SpriteBatch(GraphicsDevice); 

      // Textures 
      Texture2D texture = Content.Load<Texture2D>("Idle"); 
      Texture2D texture2 = Content.Load<Texture2D>("Run"); 
      // Animations 
      animatedSprite = new AnimatedSprite(texture, 1, 11); 
      animatedSprite2 = new AnimatedSprite(texture2, 1, 10); 
     } 

     protected override void UnloadContent() 
     { 
     } 

     protected override void Update(GameTime gameTime) 
     { 
      keyState = Keyboard.GetState(); 
      idle = true; // If the character doesn't move this will stay true 

      if (keyState.IsKeyDown(Keys.Q)) 
      { 
       position.X -= 3; 
       idle = false; 
      } 
      if (keyState.IsKeyDown(Keys.P)) 
      { 
       position.X += 3; 
       idle = false; 
      } 

      base.Update(gameTime); 
      animatedSprite.Update(); 
      animatedSprite2.Update(); 
     } 

     protected override void Draw(GameTime gameTime) 
     { 
      GraphicsDevice.Clear(Color.CornflowerBlue); 

      base.Draw(gameTime); 
      if(idle) 
       animatedSprite.Draw(spriteBatch, position); 
      else 
       animatedSprite2.Draw(spriteBatch, position); 
     } 
    } 
} 

而且應該做你想做的。另外,如果動畫有點笨重,我會推薦使用精靈表並創建自己的可以存儲所有玩家變量的動畫類。如果您決定製作遊戲多人遊戲,這非常有用。

+0

這工作。非常感謝你。 :) – Kashin 2014-09-27 16:55:13

0

可以說左右步行anmation使用5幀。它在單一紋理上。

Function Update 

    if State = Left 
     currentFrame +=1; 
     if currentFrame > 5 then currentFrame = 0 
     texureSource = new rectangle(32*currentFrame,0,32,23); 
    end if 
    if State = Right 
     currentFrame +=1; 
     if currentFrame > 5 then currentFrame = 0 
     texureSource = new rectangle(32*currentFrame,32,32,23); 
    end if 

End Function 
相關問題