Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try using SpriteSortMode.Immediate. Otherwise, I think the problem might be in the way you do things. It seems messy to leave rendering to a class outside your UI. You should be able to tell a button to draw, and it should draw itself. Or indeed just tell your UI to draw, and it draws all controls, which draws their child-controls, etc.</p> <p>That way you can build your UI in a hierarchy::)</p> <p>Structure your UI a bit; Have a base class like UIControl or something;</p> <pre><code>List&lt;UIControl&gt; 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); } </code></pre> <p>Then you have a Panel-class:</p> <pre><code>class Panel: UIControl { Vector2 Position { get; set; } Texture2D Texture { get; set; } override Draw(SpriteBatch spriteBatch) { spriteBatch.Draw(Texture, Position, Color.White); base.Draw(spriteBatch); } } </code></pre> <p>Then you have a Label-class:</p> <pre><code>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); } } </code></pre> <p>and a button-class:</p> <pre><code>class Button: Label { //Inherit label, so we dont need to create som much new code public event EventHandler&lt;EventArgs&gt; 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 &amp;&amp; Bounds.Contains(input.MousePosition)) Click(this, new EventArgs()); } } </code></pre> <p>You might have noticed that I dont have any SpriteBatch.Begin/End here? that is because:</p> <pre><code>class UI { List&lt;UIControl&gt; 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(); } } </code></pre> <p>Catch my drift?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload