5

我正在尝试垂直滚动一系列矩形。每个矩形与下一个矩形有固定的距离。第一个矩形不得低于屏幕顶部 10 像素,而最后一个矩形不得高于文本框 20 像素。换句话说,我在模仿 Windows Phone 中的 SMS 应用程序。

理论上,下面的方法应该动态地滚动矩形,虽然它确实如此,但在某些情况下,一些矩形彼此之间的距离比它们应该的更近(最终重叠)。当屏幕上的轻弹缓慢时,效果似乎被放大了。

private void Flick()
{
    int toMoveBy = (int)flickDeltaY;
    //flickDeltaY is assigned in the HandleInput() method as shown below
    //flickDeltaY = s.Delta.Y * (float)gameTime.ElapsedGameTime.TotalSeconds;

    for (int i = 0; i < messages.Count; i++)
    {
        ChatMessage message = messages[i];
        if (i == 0 && flickDeltaY > 0)
        {
            if (message.Bounds.Y + flickDeltaY > 10)
            {
                toMoveBy = 10 - message.Bounds.Top;
                break;
            }
        }
        if (i == messages.Count - 1 && flickDeltaY < 0)
        {
            if (message.Bounds.Bottom + flickDeltaY < textBox.Top - 20)
            {
                toMoveBy = textBox.Top - 20 - message.Bounds.Bottom;
                break;
            }
        }
    }
    foreach (ChatMessage cm in messages)
    {
        Vector2 target = new Vector2(cm.Bounds.X, cm.Bounds.Y + toMoveBy);
        Vector2 newPos = Vector2.Lerp(new Vector2(cm.Bounds.X, cm.Bounds.Y), target, 0.5F);
        float omega = 0.05f;
        if (Vector2.Distance(newPos, target) < omega)
        {
            newPos = target;
        }
        cm.Bounds = new Rectangle((int)newPos.X, (int)newPos.Y, cm.Bounds.Width, cm.Bounds.Height);
    }
}

我真的不了解向量,所以如果这是一个愚蠢的问题,我深表歉意。

4

1 回答 1

0

我并不完全了解您要达到的目标。但是我在您的代码中发现了一个问题:您的线性插值 ( Vector2.Lerp) 没有真正意义(或者我错过了什么?):

Vector2 target = new Vector2(cm.Bounds.X, cm.Bounds.Y + toMoveBy); // <-- the target is Y-ToMoveBy different from the actual Y position
Vector2 newPos = Vector2.Lerp(new Vector2(cm.Bounds.X, cm.Bounds.Y), target, 0.5F); // <-- this line is equivalent to say that newPos will be new Vector2(cm.Bounds.X, cm.Bounds.Y + toMoveBy / 2);
float omega = 0.05f;
if (Vector2.Distance(newPos, target) < omega) // So the only chance to happen is toMoveBy == 0.10 ??? (because you modify `cm` each time `Flick()` is called ? - see next line) 
{
    newPos = target;
}
cm.Bounds = new Rectangle((int)newPos.X, (int)newPos.Y, cm.Bounds.Width, cm.Bounds.Height); // <-- Equivalent to  new Rectangle((int)cm.Bounds.X, (int)cm.Bounds.Y + toMoveBy / 2, ...)
于 2013-06-07T08:19:14.920 回答