2017-04-25 58 views
0

目前我嘗試使用Monogame作爲框架在C#中編寫2D輪盤。 我對Monogame很有經驗,但是我從來不需要管理快速移動的紋理來粘合tho。c#Monogame/XNA快速移動時保持貼圖

這個想法是,3個紋理(黑色,紅色,綠色)在15個物體中相互連接。 這很好,只要我不開始滾動所有這些對象實際上「讓輪子移動」。

移動一段時間後,紋理不再彼此連接。他們之間有一些空間或開始重疊。 我做了一個簡短的視頻來澄清這個問題:https://puu.sh/vwbyU/d396c6ad99.mp4(這是大約10 MB,一些瀏覽器可能下載它,它顯示出來之前)

這是最重要的代碼:

const int _roulleteOffset = 170; // X coordinate to start at 
Sprite[] fieldList = new Sprite[15]; // Represents my wheel (Array of my roulette fields/textures) 
Rectangle scrollArea; // Fixed Area for the complete fieldList 
float scrollSpeed = 0.0f; // Scrollspeed of the wheel. 0 on start. 

// First I call the Content.Load to fill fieldList.Texture then 
// this is called to position the "wheel" objects 
private void AdditionalInit() 
{ 
    for (int i = 0; i < fieldList.Length; i++) 
    { 
     fieldList[i].Position = new Vector2(_roulleteOffset + i * fieldList[i].Texture.Width, Statics.GAME_WINDOW_HEIGHT/2 - fieldList[i].Texture.Height/2); 
    } 
    scrollArea = new Rectangle((int)fieldList[0].Position.X, (int)fieldList[0].Position.Y, fieldList[0].Texture.Height * fieldList.Length + 30, fieldList[0].Texture.Height); 
} 

// This Method is called in Update() - And I guess the problem has to be fixed here 
private void ScrollRoulette() 
{ 
    for (int i = 0; i < fieldList.Length; i++) 
    { 
     fieldList[i].position.X += scrollSpeed; // scrollSpeed is set with pressing + or - on keyboard 
     if (fieldList[i].Position.X >= scrollArea.Width + fieldList[i].Texture.Width) 
     { 
      // After leaving the "scrollArea" set it to the start of it (Leaving on right side and entering at the left side again) 
      fieldList[i].position.X = scrollArea.X - fieldList[i].Texture.Width; 
     } 
    } 
} 

// The part of my Draw Method. But I don't think that I made a mistake here 
public override void Draw(GameTime gameTime) 
{ 
    Statics.SPRITEBATCH.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend); 
    for (int i = 0; i < fieldList.Length; i++) 
    { 
     Statics.SPRITEBATCH.Draw(fieldList[i].Texture, fieldList[i].Position, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f); 
    } 
    Statics.SPRITEBATCH.End(); 
} 

只是看視頻來了解我在說什麼。也許你可以幫助我。即使速度較慢,紋理也隨着時間的推移開始斷開或重疊。

+0

當您設置scrollSpeed有從一開始就爲恆定值(如10),隨後又停止0直接,這是否影響依然發生? –

+0

如果不是,我認爲問題是fieldList [i] .Position.X和scrollArea.Width + fieldList [i] .Texture.Width(在ScrollRoulette方法中的條件)之間的區別將因每個字段而異,並添加此當您將其設置在下一行時,可能會有所幫助。 –

+0

這種有助於找到解決方案,但不是解決方案。如果我將速度設置爲10,則只會產生一個像〜10像素寬的小間隙。但是,當滾動這個恆定值10時,它看起來很好。只要我降低或提高速度,它會再次產生更多的間隙或重疊。 https://puu.sh/vwlk2/9660fac300.jpg這是差距,其他人在滾動大約一分鐘後看起來很好。看起來像http://puu.sh/vwlmy/da777df27e.jpg在滾動時設置速度。 – Hydrano

回答

0

在另一個德國論壇的幫助下,我解決了這個問題!感謝上帝。花了我整整一天這一行。我只需要調整傳入磁貼的位置。不只是將pos.x設置爲開始。

private void ScrollRoulette() 
    { 
     for (int i = 0; i < fieldList.Length; i++) 
     { 
      fieldList[i].position.X += scrollSpeed; 

      if (fieldList[i].Position.X >= scrollArea.Width + (fieldList[i].Texture.Width * 2)) 
      { 
       // This works now 
       fieldList[i].position.X = scrollArea.X + (fieldList[i].Position.X - (scrollArea.Width + scrollArea.X)); 
      }     
     } 
    }