繁体   English   中英

XNA使精灵跟随鼠标指针移动,但是会延迟吗?

[英]XNA make sprite follow the mouse pointer but with a delay?

在这里,我在您的帮助下更新了代码。 无论如何,它仍然没有执行应做的事情,因为鼠标指针会延迟一段时间。

气球(子画面)沿对角线飞行,一旦遇到鼠标指针便不会停止,只会减速然后继续移动然后加速。 一旦BalloonPosition等于鼠标指针,我添加了一个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;

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

您可以使用列表或数组来保留延迟。 每隔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;
    }

这是我头顶上要采取的措施:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM