【问题标题】:XNA make sprite follow the mouse pointer but with a delay?XNA 使精灵跟随鼠标指针但有延迟?
【发布时间】: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 种不同的东西。速度差或延迟差或两者兼而有之。你一直说你需要延迟,但当你解释你需要速度差异时。你真正需要哪一个?我已经通过以下答案向您展示了如何根据从鼠标到气球的距离来改变速度。我还在那个答案中解释了如何进行时间延迟,但我没有为此编写任何代码。气球几乎永远不会等于鼠标指针。由于忘记设置速度,我更新了下面的代码。

标签: c# xna


【解决方案1】:
NewPosition = Vector2.Lerp(CurrentPosition, DesiredPosition, Speed);

【讨论】:

    【解决方案2】:

    这就是我的想法:

    public List<Tuple<TimeSpan, Vector2>> Points { get; set; }
    
    public Vector2 CurrentPoint { get; set; }
    
    public void Update(GameTime gameTime)
    {
        Points.Add(new Tuple<TimeSpan, Vector2>(TimeSpan.FromSeconds(1), mouseCoords));
    
        foreach(var point in Points)
        {
            point.Item1 -= gameTime.ElapsedTimeSpan;
    
            if (point.Item1 < TimeSpan.Zero)point
                CurrentPoint = point.Item2;
        }
    }
    

    我很确定我不需要解释代码,对吧? 在绘图循环中,您只需始终绘制 CurrentPoint

    【讨论】:

      【解决方案3】:

      您可以使用列表或数组来保持延迟。每 0.03125 秒将鼠标位置存储在列表中。然后设置延迟计时器(整数计数器应该工作)。当延迟结束时,开始以与列表中相同的速度从列表中读取。 (在这种情况下为 0.03125)并将您的精灵移向该点。

      由于您正在寻找如何改变速度,这是一种方法。

          Vector2 balloonOrigin, balloonPosition;
          Vector2 balloonVelocity;
          private float MaxDistance = 1920; // Reduce this value to slow down the balloon.
      
          public static float AngleBetweenVectors(Vector2 v1, Vector2 v2) { return (float)Math.Atan2(v2.Y - v1.Y, v2.X - v1.X); }
      
          public void test()
          {
              //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 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 = AngleBetweenVectors(balloonPosition, mousePosition) * (distance / MaxDistance);
              // Set the balloons position to the new velocity.
              balloonPosition += balloonVelocity;
          }
      

      【讨论】:

      • 使用Update 的自然循环可能比你自己的计时器更好。
      • @gunr2171 这就是我所说的使用整数计数器的意思。我总是把任何计算时间的东西称为计时器。很抱歉造成混乱。
      • 有没有更好的办法?我的意思是不使用列表或数组,而是使用“矢量速度”的提示。
      • 速度本身无法提供延迟。在我看来,列表是最好的选择。您可以轻松存储和检索向量。
      • 我怀疑不能这样做,因为书上建议用这个提示与速度:“在程序中添加一个气球速度向量,并根据气球的距离改变速度到鼠标指针。”
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-22
      • 1970-01-01
      相关资源
      最近更新 更多