使用时间来平衡游戏更新

时间:2013-11-21 18:47:01

标签: c# xna

我正在尝试将我的XNA游戏的FPS上限设置为90,现在已设置,游戏以惊人的速度运行,即使我将速度乘以(float)gameTime.ElapsedGameTime.TotalMilliseconds
你能帮我弄清楚它为什么不起作用吗?我看到所有其他人都跑得那么好但我的不行。

所以这是我的Player课程(足够大):

更新方法:

public void Update(GameTime gameTime, MouseState mouse, KeyboardState keyboard, GamePadState gamepad, KeyboardState oldKeyboard)
{
    m_align = (float)gameTime.ElapsedGameTime.TotalMilliseconds / 1000; 
    move(gameTime, mouse, keyboard, gamepad); //On gere le déplacement du joueur

    //ACTIONS POSSIBLES
    if (keyboard.IsKeyDown(Keys.Space)) //TIR
    {
       m_weapon[m_activeWeapon].Shoot(ref GameMain.Bullets, m_hitbox, m_direction);
    }
    else if (keyboard.IsKeyDown(Keys.R) && oldKeyboard.IsKeyUp(Keys.R)) //RECHARGEMENT DE L'ARME ACTIVE
    {
        m_weapon[m_activeWeapon].reload();
    }

    if (keyboard.IsKeyUp(Keys.Z) //Si on ne se déplace 
        && keyboard.IsKeyUp(Keys.S)
        && keyboard.IsKeyUp(Keys.Q) 
        && keyboard.IsKeyUp(Keys.D))
    {
       this.FrameColumn = 1;
    }

    if (keyboard.IsKeyDown(Keys.H))
    {
        m_hp--;
        if (m_hp < 0)
        {
            m_hp = 0;
        }
    }

移动方法:

public void move(GameTime gameTime, MouseState mouse, KeyboardState keyboard, GamePadState gamepad)
{
    if (keyboard.IsKeyDown(Keys.Z) || gamepad.DPad.Up == ButtonState.Pressed)
    {
        m_position.Y -= m_speed * m_align;
        if (inScreen() != true || tileCollision() == true) //Si le déplacement est illégal
        {
            m_position.Y += m_speed * m_align;
            FrameColumn = 1;
        }
        else
        {
            m_direction = Direction.Up;
            Animate();
            WalkSound();
        }
    }
    else if (keyboard.IsKeyDown(Keys.S) || gamepad.DPad.Down == ButtonState.Pressed)
    {
        m_position.Y += m_speed * m_align;
        if (inScreen() != true || tileCollision() == true) //Si le déplacement est illégal
        {
            m_position.Y -= m_speed * m_align;
            FrameColumn = 1;
        }
        else
        {
            m_direction = Direction.Down;
            Animate();
            WalkSound();
        }
    }
    else if (keyboard.IsKeyDown(Keys.Q) || gamepad.DPad.Left == ButtonState.Pressed)
    {
        m_position.X -= m_speed * m_align;
        if (inScreen() != true || tileCollision() == true) //Si le déplacement est illégal
        {
            m_position.X += m_speed * m_align;
            FrameColumn = 1;
        }
        else
        {
            m_direction = Direction.Left;
            Animate();
            WalkSound();
        }
    }
    else if (keyboard.IsKeyDown(Keys.D) || gamepad.DPad.Right == ButtonState.Pressed)
    {
        m_position.X += m_speed * m_align;
        if (inScreen() != true || tileCollision() == true) //Si le déplacement est illégal
        {
            m_position.X -= m_speed * m_align;
            FrameColumn = 1;
        }
        else
        {
            m_direction = Direction.Right;
            Animate();
            WalkSound();
        }
    }
    m_hitbox.X = (int)m_position.X;
    m_hitbox.Y = (int)m_position.Y;
}

属性:

//Attributs
public Vector2 m_position;
public Rectangle m_hitbox;
Direction m_direction;
int m_speed = 3;
float m_align; //Variable d'alignement du temps

string m_name;
int m_hp;
Weapon[] m_weapon;
int m_activeWeapon;

int FrameLine;
int FrameColumn;
int animationSpeed;
SpriteEffects Effect;
int Timer; //animation frames

int SoundTimer; //Son bruit de pas
bool SoundSteps; //Son bruit de pas

1 个答案:

答案 0 :(得分:1)

默认情况下,您的更新和绘制方法限制为每秒60次调用。要更改此添加或更改为:

Game.IsFixedTimeStep = false;

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.game.isfixedtimestep.aspx