Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 处理精灵时,减缓阵列中的纹理更改。_C#_Xna 4.0 - Fatal编程技术网

C# 处理精灵时,减缓阵列中的纹理更改。

C# 处理精灵时,减缓阵列中的纹理更改。,c#,xna-4.0,C#,Xna 4.0,我正在用XNA4.0用c#做一个小游戏。我已经成功地使我的精灵向各个方向移动,并使它看起来像是在使用阵列行走。到目前为止,我只测试了“向上”键。问题是,当我按下“向上”键时,精灵会移动,但它穿过阵列中的所有元素的速度如此之快,以至于看起来他正在以极快的速度奔跑,以获得足够的空间。有没有办法减缓纹理在彼此之间的变化速度,例如暂停方法或其他方法。非常感谢您的帮助,谢谢 namespace RandomGame { public class Game1 : Microsoft.Xna.Fra

我正在用XNA4.0用c#做一个小游戏。我已经成功地使我的精灵向各个方向移动,并使它看起来像是在使用阵列行走。到目前为止,我只测试了“向上”键。问题是,当我按下“向上”键时,精灵会移动,但它穿过阵列中的所有元素的速度如此之快,以至于看起来他正在以极快的速度奔跑,以获得足够的空间。有没有办法减缓纹理在彼此之间的变化速度,例如暂停方法或其他方法。非常感谢您的帮助,谢谢

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:Microsoft.Xna.Framework.Game
{
图形管理器图形;
SpriteBatch SpriteBatch;
Color backColor=Color.FROMNONPREMPLIED(190230248250);
int i=0;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
graphics.IsFullScreen=false;
graphics.PreferredBackBufferHeight=500;
graphics.PreferredBackBufferWidth=800;
Content.RootDirectory=“Content”;
}
受保护的覆盖无效初始化()
{
base.Initialize();
}
纹理2d[]上纹理=新纹理2d[6];
纹理2D标准纹理;
纹理2D左纹理;
纹理2D右纹理;
矢量2位置=新矢量2(380230);
受保护的覆盖void LoadContent()
{
spriteBatch=新spriteBatch(图形设备);
startTexture=Content.Load(“BlueLinkStart”);
leftTexture=Content.Load(“BlueLinkLeft”);
rightTexture=Content.Load(“BlueLinkRight”);
UpTextures[0]=Content.Load(“BlueLinkUp”);
UpTextures[1]=Content.Load(“BlueLinkUp2”);
UpTextures[2]=Content.Load(“BlueLinkUp3”);
UpTextures[3]=Content.Load(“BlueLinkUp4”);
UpTextures[4]=Content.Load(“BlueLinkUp5”);
UpTextures[5]=Content.Load(“BlueLinkUp6”);
}
受保护的覆盖无效UnloadContent()
{
}
受保护覆盖无效更新(游戏时间游戏时间)
{
//允许游戏退出
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed)
这是Exit();
if(Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape))
{
这是Exit();
}
if(Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Left)和&position.X>-3)
{
位置X-=2;
}
if(Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Right)和&position.X<772)
{
位置X+=2;
}
if(Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Up)和&position.Y>-3)
{
位置Y-=2;
}
if(Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Down)和&position.Y<472)
{
位置Y+=2;
}
更新(游戏时间);
}
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色。从非倍数(188231241255));
if(Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Left))
{
spriteBatch.Begin(SpriteSortMode.BackToFront,BlendState.AlphaBlend);
绘制(左纹理、位置、颜色、白色);
spriteBatch.End();
基础。抽签(游戏时间);
}
else if(Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Right))
{
spriteBatch.Begin(SpriteSortMode.BackToFront,BlendState.AlphaBlend);
绘制(右纹理、位置、颜色、白色);
spriteBatch.End();
基础。抽签(游戏时间);
}
else if(Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Up))
{
spriteBatch.Begin(SpriteSortMode.BackToFront,BlendState.AlphaBlend);
spriteBatch.Draw(上弦[i],位置,颜色,白色);
spriteBatch.End();
i++;
如果(i==6){i=0;}
基础。抽签(游戏时间);
}
其他的
{
spriteBatch.Begin(SpriteSortMode.BackToFront,BlendState.AlphaBlend);
spriteBatch.Draw(起始结构、位置、颜色、白色);
spriteBatch.End();
基础。抽签(游戏时间);
}
}
}
}
是的,有

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

然后更新动画是
Update()
方法的任务。您显然依赖于60 Hz的更新频率。这对于小游戏来说是可以的,但是对于大游戏,你应该考虑可能的减速。如果您有
n
sprite,并且希望每
m
ms更改一次sprite,那么您将更新
animationState
,如下所示:

animationState += updateDuration * m;
if(animationState >= n) animationState -= n;
updateRation
是自上次更新以来的时间。因此,对于60赫兹,这是
1000.0/60

然后需要在
draw()方法中绘制正确的精灵:

spriteBatch.Draw(UpTextures[(int)animationState], position, Color.White);

这不会导致索引越界异常吗?不应该<代码>动画状态总是
=0
,这应该可以。