2014-09-26 2 views
0

캐릭터를 움직이게하고 왼쪽이나 오른쪽으로 걸어 가려고합니다. 기본적인 애니메이션을 만드는 방법을 배웠지 만, 그 사이를 전환하는 방법을 알 수는 없습니다.C# XNA 프로그램에서 애니메이션을 어떻게 전환합니까?

왼쪽으로 걸어 가면 '유휴'(animatedSprite) 애니메이션에서 'walking left'(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); 
     } 
    } 
} 

을 그리고 당신이 원하는 일을해야한다.

또한 애니메이션이 조금 복잡해지면 스프라이트 시트를 사용하고 모든 플레이어 변수를 저장할 수있는 자신 만의 Animation 클래스를 만드는 것이 좋습니다. 이 기능은 게임 멀티 플레이어를 만들기로 결정한 경우 매우 유용합니다.

+0

이것은 효과가 있습니다. 고맙습니다. :) – Kashin

0

왼쪽 및 오른쪽 보행 시선은 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 
관련 문제