I have 3 classes:-
1-Tile Class
2-Map Class which is inheriting from Tile Class
3-Enemy Class
Now I want to print out the variable DesertTileRec.X in Enemy class. How can I do that? when I try to print it my game crash and tells me "Object reference not set to an instance of an object". this is because I didn't call all the methods in the map class in my enemy class.
I don't want to call the methods of Map class in Enemy Class because I already did that in my Main class. I just want to print that variable without calling Map class again in my Enemy Class.
If I print DesertTileRec.X in my main class it works becuase I have called all the Map class methods.
Is there a way to pass that varible from Main class to Enemy Class?
public class Tiles
{
protected Vector2 TilePosition;
public Rectangle DesertTileRec;
public void CreateRectangleAroundTile()
{
DesertTileRec.X = (int)TilePosition.X;
DesertTileRec.Y = (int)TilePosition.Y;
DesertTileRec.Width = (int)DesertTile.Width;
DesertTileRec.Height = (int)DesertTile.Height;
}
}
class Map : Tiles
{
protected int MapX = 26, MapY = 19; // MapX =
public void Draw(SpriteBatch spriteBatch)
{
for (int x = 0; x < MapX; x++)
{
for (int y = 0; y < MapY; y++)
{
TilePosition.X = (float)x * TileX;
TilePosition.Y = (float)y * TileY;
}
}
CreateRectangleAroundTile();
}
}
class Enemy
{
Map MMap = new Map();
void Draw
{
//If I don't call Map.Draw method my game crash. I don't want to call the Map.Draw method again becuase I have alread called it in my Main class
MMap.CreateRectangleAroundTile();
spriteBatch.DrawString(Text, "X " + MMap.DesertTileRec.X + "\n" + "Y: " + MMap.DesertTileRec.Y + "\n" + "Width: " + MMap.DesertTileRec.Width + "\n" + "Height: " + MMap.DesertTileRec.Height, new Vector2(250, 100), Color.White);
}
}