【发布时间】:2014-10-30 02:22:08
【问题描述】:
在这里,我在您的帮助下更新了我的代码。 无论如何,它仍然没有做应该做的事情,跟随鼠标指针有延迟。
气球(精灵)沿对角线飞行,一旦碰到鼠标指针就不会停止,只会减速然后继续移动然后加速。 一旦气球位置等于鼠标指针,我添加了一个 if 条件,以使速度 = 0,但这不会停止气球。
我添加了一部分代码,用于将气球(精灵)保留在屏幕中。
protected override void Update(GameTime gameTime)
{
MouseState currentMouseState = Mouse.GetState();
//balloonPosition = new Vector2(currentMouseState.X, currentMouseState.Y);
//System.Windows.Input.MouseState currentMouseState = System.Windows.Input.Mouse.GetState();
// Get the current mouse position
Vector2 mousePosition = new Vector2(currentMouseState.X, currentMouseState.Y);
// Get the distance between the balloon and the mouse.
float distance = Vector2.Distance(mousePosition, balloonPosition);
// You can change the standard velocity / or the max distance to make the sprite move faster or slower.
// Currently it may move to fast or to slow for you to know a difference.
balloonVelocity = StandardVelocity * (distance/MaxDistance);
// Set the balloons position to the new velocity.
balloonPosition += balloonVelocity;
if (balloonPosition == mousePosition)
{
balloonVelocity = new Vector2(0);
}
//Keep the balloon in the screen
if (balloonPosition.X < balloon.Width / 2)
balloonPosition.X = balloon.Width / 2;
if (balloonPosition.Y < balloon.Height / 2)
balloonPosition.Y = balloon.Height / 2;
if (balloonPosition.X > Window.ClientBounds.Width - balloon.Width / 2)
balloonPosition.X = Window.ClientBounds.Width - balloon.Width / 2;
if (balloonPosition.Y > Window.ClientBounds.Height - balloon.Height / 2)
balloonPosition.Y = Window.ClientBounds.Height;
}
【问题讨论】:
-
正如我一直说的,有 3 种不同的东西。速度差或延迟差或两者兼而有之。你一直说你需要延迟,但当你解释你需要速度差异时。你真正需要哪一个?我已经通过以下答案向您展示了如何根据从鼠标到气球的距离来改变速度。我还在那个答案中解释了如何进行时间延迟,但我没有为此编写任何代码。气球几乎永远不会等于鼠标指针。由于忘记设置速度,我更新了下面的代码。