XNA使精灵跟随鼠标指针,但延迟?

时间:2014-10-27 17:32:55

标签: c# xna

我在这里用你的帮助更新了我的代码。 无论如何,它仍然没有按照鼠标指针延迟执行应该做的事情。

气球(精灵)对角飞行,一旦满足鼠标指针就不会停止,只会减速然后继续移动然后加速。 一旦balloonPosition等于鼠标指针,我就添加了一个if条件,使velocity = 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 个答案:

答案 0 :(得分:1)

NewPosition = Vector2.Lerp(CurrentPosition, DesiredPosition, Speed);

答案 1 :(得分:0)

您可以使用列表或数组来保持延迟。将鼠标位置每隔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;
    }

答案 2 :(得分:0)

这就是我的最爱:

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