2013-03-24 44 views
5

我想要垂直滚动一系列矩形。每个矩形与下一个具有固定的距离。第一个矩形不得低于屏幕顶部10个像素,而最后一个矩形不得超过文本框20个像素以上。换句话说,我模仿Windows Phone中的SMS应用程序。动力学滚动列表<Rectangle> in XNA

理论上,下面的方法应该动态地滚动矩形,而在某些情况下,有些矩形会比它们应该接近(最终重叠)。当屏幕上的轻扫速度很慢时,效果似乎会放大。

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); 
    } 
} 

我真的不明白向量,所以我很抱歉,如果这是一个愚蠢的问题。

回答

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, ...) 
+0

嗯,基本上我只是想能够滚动动力学的列表基于触摸输入的矩形对象。这是一个与线程化SMS应用程序非常类似的接口的XNA实现。我也不太明白你的答案。我添加了if条件,因为我在某处读到“Lerp”不能达到目标,只能靠近目标,所以阈值“omega”在那里,因此当它接近目标时,它就会分配它。但它可能仍然是错误的。 – 2013-06-07 10:21:08