我正在創建基於小行星的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:啊,我錯過了重置。你可能是對的 - 控制過度的速度將有助於看到輪換髮生了什麼。 –