在处理精灵时减慢数组中的纹理变化。

时间:2013-06-01 07:04:17

标签: c# xna-4.0

我正在使用XNA 4.0在c#中制作一个小游戏。我已经成功地使我的精灵向各个方向移动,并使它看起来像是在使用数组实际行走。到目前为止,我只用" Up"键。问题在于,当我按下向上键时,精灵会移动,但它会快速通过数组中的所有元素,看起来他正在为他正在前进的空间量运行。有没有办法减慢纹理在彼此之间变化的速度,例如暂停方法或其他东西。感谢任何帮助,谢谢。

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 个答案:

答案 0 :(得分:1)

是的,有。

首先,您需要一个保存动画状态的变量。你的i可能就是那个。但是,您应该将其重命名为例如animationState以反映其目的。此外,更容易将其设为floatdouble变量。

然后更新动画是Update()方法的任务。你显然依赖于60 Hz的更新频率。对于小型游戏来说这是可以的,但是对于较大的游戏,你应该考虑减慢速度。如果您有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);