C# XNA: Custom drawing class isn't printing text though it has the location right -
C# XNA: Custom drawing class isn't printing text though it has the location right -
so i'm trying draw text c# , xna. i've got spritefont
loaded in fine, no bugs in code , no compile-time warnings or errors. no errors thrown during execution.
when draw text main game class, works fine. however, i've got separate class i'm trying draw from. i've created instance of it, called constructor , draw()
function of it, yet still doesn't work.
here's main game class (the relevant bits anyway):
protected override void draw(gametime gametime) { graphicsdevice.clear(color.turquoise); if (!startscreen.hasrun) { // works fine spritebatch.begin(); spritebatch.drawstring(spaceage, "score 0", new vector2(50, 50), color.black); spritebatch.end(); // fails draw startscreen.draw(spritebatch); } else { } base.draw(gametime); }
here's draw()
function of startscreen
instance refers (created separate startscreen
class in file):
public boolean draw(spritebatch spritebatch) { if (this.hasrun == false) { spritebatch.begin(); spritebatch.draw(this.background, new rectangle(0, 0, this.screenwidth, this.screenheight), color.white); spritebatch.drawstring(this.font, "devbuild: 001", new vector2(this.textstart, 50), color.black); spritebatch.end(); /* console.writeline("hasrun: " + hasrun); console.writeline("screenwidth: " + convert.tostring(screenwidth)); console.writeline("screenheight: " + convert.tostring(screenheight)); console.writeline("dimensions.x: " + convert.tostring(stringdimensions.x)); console.writeline("dimensions.y: " + convert.tostring(stringdimensions.y)); console.writeline("textstart: " + convert.tostring(textstart)); */ // logs above give right output this.hasrun = true; console.writeline("hasrun: " + hasrun); homecoming true; } else homecoming false; }
so why, when function has right on-screen coordinates draw text @ ( (textstart, 50)
textstart
calculates 312 on 800-wide screen), , phone call drawstring
same format, text not show up?
feel free inquire more code if it's necessary.
(n.b. phone call spritebatch.draw
here doesn't work either, image doesn't drawn - perhaps same issue?)
edit: i've tried suggestion below: removed begin()
, end()
startscreen class , called startscreen.draw()
within game's draw()
function in between phone call begin()
, end()
. still nil gets drawn.
instead of making draw method public bool create public void , have variable bool should work , code should this:
bool drawn = false; // defined @ top of class draw method doesn't need bool public void draw(spritebatch spritebatch) { if (this.hasrun == false) { spritebatch.begin(); spritebatch.draw(this.background, new rectangle(0, 0, this.screenwidth,this.screenheight), color.white); spritebatch.drawstring(this.font, "devbuild: 001", new vector2(this.textstart, 50), color.black); spritebatch.end(); /* console.writeline("hasrun: " + hasrun); console.writeline("screenwidth: " + convert.tostring(screenwidth)); console.writeline("screenheight: " + convert.tostring(screenheight)); console.writeline("dimensions.x: " + convert.tostring(stringdimensions.x)); console.writeline("dimensions.y: " + convert.tostring(stringdimensions.y)); console.writeline("textstart: " + convert.tostring(textstart)); */ // logs above give right output this.hasrun = true; console.writeline("hasrun: " + hasrun); drawn = true; } else { drawn = false; } }
also why draw method need have bool @ code have given doesn't used anywhere
c# xna xna-4.0
Comments
Post a Comment