2013-03-24 41 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