2015-09-04 42 views
2

因此,这里是我的代码:C#XNA - 通过项目迭代和画他们

// Variables 
private SpriteFont font; 
private Vector2 fontPos, fontOrigin; 
private List<string> menuItems; 

// LoadContent() 
font = Content.Load<SpriteFont>("myFont"); 
fontPos = new Vector2(1920/2, 1080/2); 
menuItems = new List<string>(); 
menuItems.Add("Single Player"); 
menuItems.Add("Multi Player"); 
menuItems.Add("Achievements"); 
menuItems.Add("Options"); 
menuItems.Add("Quit Game"); 

// Draw() 
for (int i = 0; i < menuItems.Count; i++) 
{ 
    Vector2 fontOrigin = Game.gameFontLarge.MeasureString(menuItems[i])/2; 
    spriteBatch.DrawString(Game.gameFontLarge, menuItems[i], new Vector2(ScreenManager.Instance.Dimensions.X/2, ScreenManager.Instance.Dimensions.Y/2), Game.NoTint, 0.0f, fontOrigin, 1.0f, SpriteEffects.None, 0.0f); 
} 

到目前为止,这个代码绘制的一切,但所有的文本都绘制在彼此在同一个坐标,屏幕的中心。如何使列表中的每个字符串都绘制在前一个字符下方,如前面的24像素?

回答

1

如果我正确地阅读你的问题,当你绘制你的字符串时,你可以简单地为每个字符串的y值添加一个固定的像素量基于其索引。

所以你的行这里:

spriteBatch.DrawString(. . . , new Vector2(ScreenManager.Instance.Dimensions.X/2, ScreenManager.Instance.Dimensions.Y/2), . . .); 

变得更加像下面这样:

spriteBatch.DrawString(. . . , new Vector2(ScreenManager.Instance.Dimensions.X/2, (ScreenManager.Instance.Dimensions.Y/2) + 24 * i), . . .); 

希望这个解决方案适用于你 - 我不完全熟悉XNA,但是这应该是大概的概念。如果它给你一个类型不匹配的错误,你可能需要使用(float)i代替i

+0

谢谢!它的工作原理不需要'我'来浮动。正如我测试过的,'Vector2.Y'和'Vector2.X'是'float',但我不必在分区中投出'2',因为浮点数可以被整数分开/相乘(可能不是在所有情况下,但这个确切情况并不需要剧组)。 – jacksparrow