2

所以几个月前我刚开始学习 C# 代码,我想在一个完整的步行周期中获得一个精灵。

我在 PNG 图像上进行了步行循环。这显然是一系列单独的图像,显​​示了步行周期的不同阶段。现在我已将其纳入我的项目/解决方案中,我在 Visual Studio 2010 中将其作为 Windows Game 4.0 进行。

而且我已经走了很远,所以我开始尝试设置 srcRect 和 destRect 。但我设法在屏幕上显示的只是图像从左到右快速滑动,然后无限向右移动。我通过使用 srcRect.X += srcRect.width 线实现了这一点。

但我想做的是让这个动作慢得多。当拇指杆向右推时,我希望 destRect 在屏幕上向右移动,并且在发生这种情况时,我希望它在我的步行周期中向右滑动。

我变得非常混乱,我是一个初学者,所以对此的任何帮助将不胜感激!以下是我到目前为止的代码!


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 fra_walk
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        GamePadState pad1, oldpad1;

        Rectangle srcRect;

        struct Rect
        {
        public Rectangle destRect;
        public Rectangle frame;
        public Point velocity;

        }
            Rect destRect;

        struct Wright
        {
            public Texture2D txr;
            public Rectangle frame;
        }

        Wright walkright;

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


            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 600;

        }

        /// <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()
        {
            // TODO: Add your initialization logic here

            destRect.velocity = new Point(0, 0);

            destRect.frame = new Rectangle(400, 400, 83, 170);

            srcRect = new Rectangle(0, 0, 64, 170);

            base.Initialize();
        }

        /// <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);



            walkright.txr = Content.Load<Texture2D>("walk right");

            // 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


            GamePadState pad1 = GamePad.GetState(PlayerIndex.One);


            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            srcRect.X += srcRect.Width;

        //  if ((pad1.ThumbSticks.Left.X == 1.0f)
         //     && (oldpad1.ThumbSticks.Left.X == 1.0f))
       //   {
                // srcRect.X += destRect.frame.Width;
        //  }

            oldpad1 = pad1;
            // TODO: Add your update logic here

            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.Draw(walkright.txr, destRect.frame, srcRect, Color.White);
            spriteBatch.End();

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}

4

1 回答 1

0

一个简单的方法来做你想要的可能是实现一个计数器。

在代码顶部添加诸如 public double animCounter = 0 之类的内容。

然后围绕 srcRect.X += srcRect.Width; 行添加一些东西的效果:

        if (animCounter > 0)
        {
            animCounter -= gameTime.ElapsedGameTime.TotalSeconds;
        }
        else
        {
            srcRect.X += srcRect.Width;
            animCounter = 0.5f;
        }

同样,这是做你想做的事情的简单方法,但不一定是最好的。一方面,+= srcRect.Width 将很快超过你的精灵表的宽度,所以你需要检查并纠正它。正如 itsme86 所建议的,您应该在 Google 上搜索一些教程。

这不是一个糟糕的开始:http: //msdn.microsoft.com/en-us/library/bb203866.aspx

于 2013-01-25T17:21:14.687 回答