2017-04-25 26 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)); 
      }     
     } 
    }