XNA DrawString()仅绘制部分字符串
我在XNA
遇到DrawString()
问题。 我为多个逻辑层使用多个SpriteBatches
。 例如:背景,对象,菜单等。
在我的菜单批处理中,我绘制了一个菜单(背景中的大灰色框),按钮(菜单上较小的灰色框)和按钮的字符串。
问题: http : //ompldr.org/vaGw4YQ/Unbenannt.png
但由于某种原因,字符串不是完全绘制的。 有谁知道为什么?
编辑:
_menuLayer.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend); if (_menu != null) { _menuLayer.Draw(_menuBoard, new Vector2(graphics.PreferredBackBufferWidth / 2 - 160, graphics.PreferredBackBufferHeight / 2 - 240), Color.White); } _menuLayer.End(); _buttonLayer.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend); if (_menu != null) { foreach (Button button in _menu.Buttons) { if (button.Pressed) { _buttonLayer.Draw(_menuButtonPressed, button.Location, Color.White); _buttonLayer.DrawString(_text, button.Text, button.GiveStringLocation(_text), Color.Black); } else { _buttonLayer.Draw(_menuButton, button.Location, Color.White); _buttonLayer.DrawString(_text, button.Text, button.GiveStringLocation(_text), Color.Black); } } } _buttonLayer.End(); Arial 20 0 true Bold <!-- * --> ~
尝试使用SpriteSortMode.Immediate。 否则,我认为问题可能在于你做事的方式。 将渲染留给UI之外的类似乎很麻烦。 你应该能够告诉按钮绘制,它应该绘制自己。 或者确实只是告诉你的UI绘制,它绘制所有控件,绘制他们的子控件等。
这样您就可以在层次结构中构建UI::)
稍微构建您的UI; 有像UIControl之类的基类;
List Children { get; protected set; } Update(MyInputSystem input, float dt) { UpdateChildren(input, dt); } UpdateChildren(MyInputSystem input, float dt) { foreach(var child in Children) child.Update(input, dt); } Draw(SpriteBatch spriteBatch) { DrawChildren(spriteBatch); } DrawChildren(SpriteBatch spriteBatch) { foreach(var child in Children) child.Draw(spriteBatch); }
然后你有一个Panel-class:
class Panel: UIControl { Vector2 Position { get; set; } Texture2D Texture { get; set; } override Draw(SpriteBatch spriteBatch) { spriteBatch.Draw(Texture, Position, Color.White); base.Draw(spriteBatch); } }
然后你有一个Label类:
class Label: Panel{ SpriteFont Font { get; set; } String Text { get; set; } Vector2 TextOffset { get; set; } Color TextColor { get; set; } override Draw(SpriteBatch spriteBatch) { spriteBatch.Draw(Texture, Position, Color.White); spriteBatch.DrawString(Font, Text, Position + TextOffset,Text Color); DrawChildren(spriteBatch); } }
和按钮类:
class Button: Label { //Inherit label, so we dont need to create som much new code public event EventHandler Click; Rectangle _Bounds public Rectangle Bounds { get { if (_Rectangle.Width == 0) _Rectangle = new Rectangle((int)Position.X,(int)Position.Y, Texture.Width, Texture.Height); return _Texture; } } override Update(MyInputSystem input, flaot dt) { if (input.MouseClicked && Bounds.Contains(input.MousePosition)) Click(this, new EventArgs()); } }
您可能已经注意到我在这里没有任何SpriteBatch.Begin / End? 那是因为:
class UI { List Controls; Update(MyInputSystem input, flioat dt) { foreach(var control in Controls) control.Update(input, dt); } Draw(SpriteBatch spriteBatch) { spriteBatch.Begin(/*Parameters*/); foreach(var control in Controls) control.Draw(spriteBatch); spriteBatch.End(); } }
抓住我的漂移?
这可能是您正在使用的字体的问题。 确保您使用的字体是“.ttf”并尝试一些不同的字体。 我强烈建议您使用Microsoft推荐的字体之一。 链接: http : //go.microsoft.com/fwlink/?LinkId = 104778&clcid = 0x409