XNA: Need help with Animations.
-
2012년 6월 13일 수요일 오후 10:13
I'm currently working on a game using xna and I'm having trouble with animations. Instead of showing the animation it shows the sprite sheet.
Here is my code:
Animation.cs:
using System; using Microsoft.Xna.Framework.Graphics; namespace Mystery { /// <summary> /// Represents an animated texture. /// </summary> /// <remarks> /// Currently, this class assumes that each frame of animation is /// as wide as each animation is tall. The number of frames in the /// animation are inferred from this. /// </remarks> public class Animation { /// <summary> /// All frames in the animation arranged horizontally. /// </summary> public Texture2D Texture { get { return texture; } } Texture2D texture; /// <summary> /// Duration of time to show each frame. /// </summary> public float FrameTime { get { return frameTime; } } float frameTime; /// <summary> /// When the end of the animation is reached, should it /// continue playing from the beginning? /// </summary> /// <summary> /// Gets the number of frames in the animation. /// </summary> public int FrameCount = 2; /// <summary> /// Gets the width of a frame in the animation. /// </summary> public int FrameWidth = 111; /// <summary> /// Gets the height of a frame in the animation. /// </summary> public int FrameHeight = 111; /// <summary> /// Constructors a new animation. /// </summary> public Animation(Texture2D texture, float frameTime) { this.texture = texture; this.frameTime = frameTime; } } }
Game1.cs:
using System; using System.Diagnostics; 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 Mystery { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Player player; Texture2D mainBackground; Animation leftwalk; Stopwatch jumpwatch = new Stopwatch(); Boolean jumpready; KeyboardState currentKeyboardState; KeyboardState previousKeyboardState; float movespeed; float fallspeed; Boolean walkleft; private float time; private int frameIndex = 1; private Rectangle viewRect; TimeSpan walk; Random random; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } /// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { graphics.PreferredBackBufferWidth = 1024; graphics.PreferredBackBufferHeight = 500; graphics.ApplyChanges(); player = new Player(); movespeed = 2.5f; fallspeed = movespeed * 3; base.Initialize(); random = new Random(); } /// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); Vector2 playerPosition = new Vector2(GraphicsDevice.Viewport.TitleSafeArea.X, GraphicsDevice.Viewport.TitleSafeArea.Y + 250 + GraphicsDevice.Viewport.TitleSafeArea.Height / 2); player.Initialize(Content.Load<Texture2D>("larry"), playerPosition); leftwalk = new Animation(Content.Load<Texture2D>("walkleft"), 0.1f); // TODO: use this.Content to load your game content here } /// <summary> /// UnloadContent will be called once per game and is the place to unload /// all content. /// </summary> protected override void UnloadContent() { // TODO: Unload any non ContentManager content here } /// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); // TODO: Add your update logic here // Save the previous state of the keyboard and game pad so we can determinesingle key/button presses previousKeyboardState = currentKeyboardState; // Read the current state of the keyboard and gamepad and store it currentKeyboardState = Keyboard.GetState(); //Update the player UpdatePlayer(gameTime); 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.Gray); spriteBatch.Begin(); if (walkleft == true) { spriteBatch.Draw(leftwalk.Texture, player.Position, Color.White); } else if (walkleft == false) { spriteBatch.Draw(leftwalk.Texture, player.Position, null, Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.FlipHorizontally, 0); } // Stop drawing spriteBatch.End(); // TODO: Add your drawing code here base.Draw(gameTime); } private void Animate(GameTime gametime) { time += (float)gametime.ElapsedGameTime.TotalSeconds; while (time > leftwalk.FrameTime) { time -= leftwalk.FrameTime; frameIndex = (frameIndex + 1) % leftwalk.FrameCount; } viewRect = new Rectangle((frameIndex * leftwalk.FrameWidth), 0, leftwalk.FrameWidth, leftwalk.FrameHeight); } private void UpdatePlayer(GameTime gameTime) { if (currentKeyboardState.IsKeyDown(Keys.Left)) { walkleft = true; player.Position.X -= movespeed; if (currentKeyboardState.IsKeyDown(Keys.LeftShift)) { // player.Position.X -= movespeed * 2; player.health = player.health - 10; // player.Initialize(Content.Load<Texture2D>("larrywalkl"), player.Position); } } if (currentKeyboardState.IsKeyDown(Keys.Right)) { walkleft = false; player.Position.X += movespeed; // player.Initialize(Content.Load<Texture2D>("larrywalkr"), player.Position); if (currentKeyboardState.IsKeyDown(Keys.LeftShift)) { // player.Initialize(Content.Load<Texture2D>("larrywalkr"), player.Position); player.Position.X += movespeed * 2; } } if (currentKeyboardState.IsKeyUp(Keys.Right) && currentKeyboardState.IsKeyUp(Keys.Left)) { player.Initialize(Content.Load<Texture2D>("larry"), player.Position); } player.Position.X = MathHelper.Clamp(player.Position.X, 0, GraphicsDevice.Viewport.Width - player.Width); player.Position.Y = MathHelper.Clamp(player.Position.Y, 0, GraphicsDevice.Viewport.Height - player.Height); } } }
Hopefully someone can help.
- 이동됨 Bob ShenMicrosoft Contingent Staff 2012년 6월 15일 금요일 오전 5:12 (From:Visual C# General)
모든 응답
-
2012년 6월 15일 금요일 오전 5:12
Hi pielegacy,
You can consider posting it at the following more appropriate forum for more efficient responses. Thanks!
XNA http://forums.create.msdn.com/forums/default.aspx?GroupID=6
Bob Shen [MSFT]
MSDN Community Support | Feedback to us

