c#Monogame / XNA保持纹理在快速移动时粘在一起

时间:2017-04-25 23:44:18

标签: c# object xna textures monogame

目前我尝试使用Monogame作为框架在C#中编写2D轮盘。 我对Monogame很安静,但我从来没有必要管理快速移动的纹理以便粘在一起。

这个想法是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();
}

观看视频,了解我在说什么。也许你可以帮助我。即使在慢速下,纹理也会随着时间的推移而开始断开或重叠。

1 个答案:

答案 0 :(得分: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) );
            }                
        }
    }