我正在创建基于小行星的2D游戏。在那场比赛中,我需要朝一个方向推动船。Monogame朝着方向
我可以画船,让它转身。但是当谈到把它向前移动时,我的问题就出现了。
我似乎没有能够得到我的头。 (这整个制作游戏的是新的我^^)
的player.cs
protected Vector2 sVelocity;
protected Vector2 sPosition = Vector2.Zero;
protected float sRotation;
private int speed;
public Player(Vector2 sPosition)
: base(sPosition)
{
speed = 100;
}
public override void Update(GameTime gameTime)
{
attackCooldown += (float)gameTime.ElapsedGameTime.TotalSeconds;
// Reset the velocity to zero after each update to prevent unwanted behavior
sVelocity = Vector2.Zero;
// Handle user input
HandleInput(Keyboard.GetState(), gameTime);
if (sPosition.X <= 0)
{
sPosition.X = 10;
}
if (sPosition.X >= Screen.Instance.Width)
{
sPosition.X = 10;
}
if(sPosition.Y <= 0)
{
sPosition.Y = 10;
}
if (sPosition.Y >= Screen.Instance.Height)
{
sPosition.Y = 10;
}
// Applies our speed to velocity
sVelocity *= speed;
// Seconds passed since iteration of update
float deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
// Multiplies our movement framerate independent by multiplying with deltaTime
sPosition += (sVelocity * deltaTime);
base.Update(gameTime);
}
private void HandleInput(KeyboardState KeyState, GameTime gameTime)
{
if (KeyState.IsKeyDown(Keys.W))
{
//Speed up
speed += 10;
sVelocity.X = sRotation; // I know this is wrong
sVelocity.Y = sRotation; // I know this is wrong
}
else
{
//Speed down
speed += speed/2;
}
if (KeyState.IsKeyDown(Keys.A))
{
//Turn left
sRotation -= 0.2F;
if (sRotation < 0)
{
sRotation = sRotation + 360;
}
}
if (KeyState.IsKeyDown(Keys.D))
{
//Turn right
sRotation += 0.2F;
if (sRotation > 360)
{
sRotation = sRotation - 360;
}
}
}
我是否关闭,或从右严重多远?
你做'SVELOCITY * = speed'在每一帧:如果'speed'是'10'并且'sVelocity'是'(0,1)',你将*非常*快速地在'(0,100000000000000)'处移动(即:在你要移动的3个帧中每帧100万单位)。我怀疑这条线是你意想不到的运动方面最大的罪魁祸首...... –
@DanPuzey他还有'sVelocity = Vector.Zero'每一帧。这可能是“速度”本身失控。 – dureuill
@dureuill:啊,我错过了重置。你可能是对的 - 控制过度的速度将有助于看到轮换发生了什么。 –