0

关于在菜单中绘制 XNA 上的字符串列表的两个问题。首先是你如何左对齐文本而不是居中对齐呢?其次是你如何在选择任何一个之前显示一个说一行?这是我到目前为止的代码;

    public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
    {
        Color color;
        int linePadding = 3;

        if (gameTime.TotalGameTime.TotalSeconds <= 3)
        {
            spriteBatch.Draw(mTheQuantumBros2, new Rectangle(300, 150, mTheQuantumBros2.Width, mTheQuantumBros2.Height), Color.White);
        }
        else
        {
            for (int i = 0; i < buttonList.Count; i++)
            {
                color = (i == selected) ? Color.Gold : Color.LawnGreen;
                spriteBatch.DrawString(spriteFont, buttonList[i], new Vector2((screenWidth / 2) - (spriteFont.MeasureString(buttonList[i]).X / 2), (screenHeight / 2) - (spriteFont.LineSpacing * (buttonList.Count) / 2) + ((spriteFont.LineSpacing + linePadding) * i)), color);
            }
        }
    }
4

1 回答 1

1

嗯..老实说,我认为中心对齐会更好看,但是在您的代码中,您需要做的就是将绘制字符串更改为:

for (int i = 0; i < buttonList.Count; i++)
{
     color = (i == selected) ? Color.LawnGreen : Color.Gold;
     spriteBatch.DrawString(spriteFont, buttonList[i], new Vector2((screenWidth / 2) /*- (spriteFont.MeasureString(buttonList[i]).X / 2)*/, (screenHeight / 2) - (spriteFont.LineSpacing * (buttonList.Count) / 2) + ((spriteFont.LineSpacing + linePadding) * i)), color);
}

你可以看到我评论的内容。通过取出它,您不再将字符串向左移动其宽度的一半。

至于第二部分,您需要做的就是检查是否选择了该值,因为您已经在确定菜单项颜色。如果选中,只需添加“|” 到字符串的开头和结尾:

for (int i = 0; i < buttonList.Count; i++)
{
     color = (i == selected) ? Color.LawnGreen : Color.Gold;
     if(i != selected)
     {
         spriteBatch.DrawString(spriteFont, buttonList[i], new Vector2((screenWidth / 2) /*- (spriteFont.MeasureString(buttonList[i]).X / 2)*/, (screenHeight / 2) - (spriteFont.LineSpacing * (buttonList.Count) / 2) + ((spriteFont.LineSpacing + linePadding) * i)), color);
     }
     else
     {
         spriteBatch.DrawString(spriteFont,"|" + buttonList[i] + "|", new Vector2((screenWidth / 2) - (int)spriteFont.MeasureString("|").X, (screenHeight / 2) - (spriteFont.LineSpacing * (buttonList.Count) / 2) + ((spriteFont.LineSpacing + linePadding) * i)), color);
     }
}

现在,由于您要添加“|” 到字符串的开头,这将影响位置。通过减去“|”的测量宽度,然后将字符串重新居中。

于 2013-05-14T22:42:20.233 回答