I’m trying to write some code that displays my player’s X value in text.
It tells me that the Microsoft.Xna.Framework.Graphics.SpriteBatch.DrawString line needs an object reference. Any ideas? Here is my code:
public void Effects(Player player)
{
string compassString = "";
int playerY = (int) (((player.position.X + player.width) * 2f) / 16f);
if (playerY > 0)
{
compassString = "Distance: " + playerY + " feet left";
if (playerY == 1)
{
compassString = "Distance: " + playerY + " foot left";
}
}
else if (playerY < 0)
{
playerY *= -1;
compassString = "Distance: " + playerY + " feet right";
if (playerY == 1)
{
compassString = "Distance: " + playerY + " foot right";
}
}
else
{
compassString = "Distance: Level";
}
Color black;
black.R = (byte)((0xff + black.R) / 2);
black.G = (byte)((0xff + black.R) / 2);
black.B = (byte)((0xff + black.R) / 2);
Microsoft.Xna.Framework.Graphics.SpriteBatch.DrawString(Main.fontMouseText, compassString, new Vector2((float) (0x16), (float) ((0x4a + (0x16)) + 0x30)), black, 0f, new Vector2(), (float) 1f, SpriteEffects.None, 0f);
}
If you want to display the coordinates as string in XNA you have to:
SpriteFontin your application.Game.Draw()method (and not just somewhere in your code!) use the defaultspriteBatchinstance to draw the coordinates using the just definedSprinteFont.Here is a good tutorial on these steps from MSDN webpage.
Here is the quote from above linked page: