As I'm currently making a 2D game, I'm trying to make it possible for my character to load between levels but I'm having a bit of an issue. The first level that my player starts on is loaded in an array with no issue at all in the Game1
class like this:
secondLevel secondLev; // instance for secondLevel class
Map map; // instance for Map class
secondLev = new secondLevel(); // used in the Initialize() function
protected override void LoadContent()
{
map.Generate(new int[,] {
// 0 = no tile drawn
// 3 = tile is drawn
{0,0,0,0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,0,0,0,},
{3,3,3,3,3,3,3,3,3,3,},
{3,3,3,3,3,3,3,3,3,3,},
}, 57); // size
}
So to be able to load the second level, I have attempted to make a new class called secondLevel
that holds the new level array and simply loads the array in its Load()
function, like so:
class secondLevel
{
Map map;
public void Initialize()
{
map = new Map();
}
public void Load()
{
map.Generate(new int[,] {
// 0 = no tile drawn
// 3 = tile is drawn
{0,0,0,0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,0,0,0,},
{3,3,3,3,3,3,3,3,3,3,},
{3,3,3,3,3,3,3,3,3,3,},
{3,3,3,3,3,3,3,3,3,3,},
{3,3,3,3,3,3,3,3,3,3,},
}, 57); // size
}
}
Now in my Game1
class, I have placed an if statement
that checks if the player has collided with the spike that loads the next level, like so:
if (player.Bounds.Intersects(spike1.Bounds)) // if player intersects with spike
{
secondLev.Load();
}
But when my player intersects with the spike, my game freezes and I get the error message: Object reference not set to an instance of an object
.
What is my issue?
If I'm missing any additional code to my question that could help you fix this, please let me know!