C# 绘图图形菜单问题
我正试图用XNA编写一个Windows游戏,到目前为止我一直在画菜单。这是我的密码: Game.cs:C# 绘图图形菜单问题,c#,user-interface,xna,xna-4.0,C#,User Interface,Xna,Xna 4.0,我正试图用XNA编写一个Windows游戏,到目前为止我一直在画菜单。这是我的密码: Game.cs: protected override void LoadContent() { Text musicLevelText; musicLevelText.textValue = "Music :"; musicLevelText.location = new Vector2(400 - (int)font.MeasureString(m
protected override void LoadContent()
{
Text musicLevelText;
musicLevelText.textValue = "Music :";
musicLevelText.location = new Vector2(400 - (int)font.MeasureString(musicLevelText.textValue).X - 20, 50);
musicLevelText.font = font;
Text soundEffectText;
soundEffectText.textValue = "Sound Effects :";
soundEffectText.location = new Vector2(400 - (int)font.MeasureString(soundEffectText.textValue).X - 20, 10 + musicLevelText.location.Y
+ (int) font.MeasureString(musicLevelText.textValue).Y);
soundEffectText.font = font;
Text languageText;
languageText.textValue = "Language :";
languageText.location = new Vector2(400 - (int)font.MeasureString(languageText.textValue).X - 20, 10 + soundEffectText.location.Y
+ (int)font.MeasureString(soundEffectText.textValue).Y);
languageText.font = font;
Button backToMainMenuButton = new Button(font, spriteBatch, "Back", new Vector2(20, 440));
optionsMenu = new Menu(new Button[1] {backToMainMenuButton}, new Text [3]{musicLevelText, soundEffectText, languageText}, background, spriteBatch);
}
Menu.cs:
public void Update()
{
for (int i = 0; i < buttons.Length; i++)
{
buttons[i].Update();
}
}
public void Draw()
{
spriteBatch.Begin();
spriteBatch.Draw(background, new Rectangle(0, 0, 800, 480), Color.White);
spriteBatch.End();
if (!menuOn)
return;
for ( int i = 0; i < buttons.Length; i++){
buttons[i].Clickable = true;
}
spriteBatch.Begin();
for (int i = 0; i < texts.Length; i++)
{
spriteBatch.DrawString(texts[i].font, texts[i].textValue, texts[i].location, Color.White);
}
spriteBatch.End();
}
public struct Text
{
public string textValue;
public Vector2 location;
public SpriteFont font;
}
在编写Menu类之前,我只是对每个按钮使用
Draw()
,工作正常。然后,出现了一些问题,我不知道为什么,但我无法显示选项菜单
。我做错了什么。有人能告诉我这个代码出了什么问题以及为什么我不能显示菜单吗?从我看到的情况来看,只有当按钮是“可点击的”时,你才调用DrawButton
功能。。。是吗?同时尽量避免调用spriteBatch.Begin()
和spriteBatch.End()代码>到多次。在主绘图函数中只使用它们一次,并在它们之间调用其他绘图函数。此外,此函数可疑:if(!menuOn)return代码>。。。你必须一步一步地进入调试器…你说“我不能显示选项菜单”
是什么意思?你已经在画另一个菜单了吗?您是否正确调用了选项menu.Draw
?正如Davor所写,要做的第一件事是使用调试器:尝试添加一些断点。如果您的背景是绘图,但按钮不是,那么肯定是“menuOn”布尔值仍然为false。另外,我只想重复一下Davor所说的:在spritebatch上调用Begin()和End(),每次调用次数尽可能少。多次处理End()所需的时间比一次要长得多。@pinckerman我实际上在定义Options Menu时就将menuOn设置为true。顺便问一下,我如何在VS2010 Ultimate上设置断点?
public void Draw()
{
if (clickable)
DrawButton(textRectangle);
}
private void DrawButton(Rectangle boundaries)
{
Vector2 size = font.MeasureString(text);
float xScale = (boundaries.Width / size.X);
float yScale = (boundaries.Height / size.Y);
// Taking the smaller scaling value will result in the text always fitting in the boundaires.
float scale = Math.Min(xScale, yScale);
// Figure out the location to absolutely-center it in the boundaries rectangle.
int strWidth = (int)Math.Round(size.X * scale);
int strHeight = (int)Math.Round(size.Y * scale);
Vector2 position = new Vector2();
position.X = (((boundaries.Width - strWidth) / 2) + boundaries.X);
position.Y = (((boundaries.Height - strHeight) / 2) + boundaries.Y);
// A bunch of settings where we just want to use reasonable defaults.
float rotation = 0.0f;
Vector2 spriteOrigin = new Vector2(0, 0);
float spriteLayer = 0.0f; // all the way in the front
SpriteEffects spriteEffects = new SpriteEffects();
// Draw the string to the sprite batch!
Color color;
if (clicked && mouse.LeftButton == ButtonState.Pressed)
{
color = Color.Silver;
}
else
{
color = Color.White;
}
spriteBatch.Begin();
spriteBatch.DrawString(font, text, position, color, rotation, spriteOrigin, scale, spriteEffects, spriteLayer);
spriteBatch.End();
}
}