我试图让我的游戏使用增量时间,以便它独立于 FPS。我尝试这样做并并排比较它们:
但是 144fps 上的播放器仍然更快。我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace _2D_Game_Test.GameStates
{
public class TestState : GameState
{
Vector2 velocity;
Vector2 position;
double change = 0;
KeyboardState keyboardState;
public override void Initialize()
{
velocity = new Vector2(0, 0);
position = new Vector2((1280 / 2) - (Game.Bob.Width / 2), (720 / 2) - (Game.Bob.Height / 2));
}
public override void Update(GameTime gameTime)
{
if (Mouse.GetState().LeftButton == ButtonState.Pressed)
{
change = 0;
velocity = new Vector2(0, 0);
position = new Vector2((1280 / 2) - (Game.Bob.Width / 2), (720 / 2) - (Game.Bob.Height / 2));
}
double delta = gameTime.ElapsedGameTime.TotalSeconds;
keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.A))
{
if (change > 0)
{
change -= (10 * delta);
}
else if (change > -5f)
{
if (change - (5 * delta) < -5f)
{
change = -5f;
}
else
{
change -= (5 * delta);
}
}
}
else if (keyboardState.IsKeyDown(Keys.D))
{
if (change < 0)
{
change += (10 * delta);
}
else if (change < 5f)
{
if (change + (5 * delta) > 5f)
{
change = 5f;
}
else
{
change += (5 * delta);
}
}
}
我做错了什么?
茅侃侃
相关分类