c# - 在 XNA 中动态滚动 List<Rectangle>

标签 c# windows-phone-7 xna

我正在尝试垂直滚动一系列矩形。每个矩形与下一个矩形之间的距离是固定的。第一个矩形距离屏幕顶部绝不能低于 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);
    }
}

我真的不懂 Vectors,所以如果这是一个愚蠢的问题,我深表歉意。

最佳答案

我真的不完全理解你想要达到的目标。但我在您的代码中发现了一个问题:您的线性插值 (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, ...)

关于c# - 在 XNA 中动态滚动 List<Rectangle>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15599705/

相关文章:

c# - Windows Phone 7 导航传递参数

c# xna : real time 2d Generation. 包括 GPU?使用 hlsl 还是 bramaha?

c# - String.Format 和 string.Format(以及原始数据类型的其他静态成员)之间有什么区别?

c# - 使用列表和并行循环时出现异常

c# - 24 小时后的 asp.net c# 网站表单操作

c# - PC 上 WP7 模拟器的独立存储在哪里?

c# - 为什么我的内存使用量在停止下载后激增?

windows - 如何更改 XNA 中的图标

c# - 创建具有频率的汽车声音

c# - 可以应用什么设计模式来构建需要来自两个数据读取器的数据的对象?