The declaration of the Rectangle object fails with an NullReferenceException, maybe it has to do with the for loops (yMax and xMax are in tiles unit) Thanks helping me catching why is this giving me an exception.
TileData[][] tile = GameMain.Level.getCollisionTiles();
int xMax = GameMain.Level.getMapHeight();
int yMax = GameMain.Level.getMapWidth();
for (int x = 0; x <= xMax; x++)
{
for (int y = 0; y <= yMax; y++)
{
Rectangle tileRectangle = tile[x][y].Target; //THIS LINE FAILS !!!!
if (tileRectangle.Contains(m_hitbox)) //Si il y a collision
{
if ((m_hitbox.X + m_hitbox.Width) > tileRectangle.X) //si le joueur percute par la gauche
{
m_hitbox.X--;
}
else if (m_hitbox.X < (tileRectangle.X + tileRectangle.Width)) //Droite
{
m_hitbox.X++;
}
if ((m_hitbox.Y + m_hitbox.Height) > tileRectangle.Y) //si le joueur percute par le haut
{
m_hitbox.Y--;
}
else if (m_hitbox.Y < (tileRectangle.Y + tileRectangle.Height)) //Bas
{
m_hitbox.Y++;
}
}
}
}
Here is some code,
the accessor (GameMain.Level.) :
public TileData[][] getCollisionTiles()
{
return m_collisionTiles;
}
the class "Level" attributes and constructor:
//Atributs
Map m_map;
List<TileLayer> m_layers;
TileLayer m_collisionLayer;
TileData[][] m_collisionTiles;
int m_mapWidth;
int m_mapHeight;
int m_tileWidth;
int m_tileHeight;
//Constructeur
public Level(ContentManager content, string levelName)
{
m_map = content.Load<Map>("Maps/"+levelName); //On charge la map
//params
m_mapWidth = m_map.Width;
m_mapHeight = m_map.Height;
m_tileWidth = m_map.TileWidth;
m_tileHeight = m_map.TileHeight;
//tiles / layers
m_layers = new List<TileLayer>(m_map.TileLayers); //On charge les calques / couches
m_collisionLayer = m_layers.Find( x => x.Name == "Collision"); //On charge le calque de collision
m_collisionTiles = m_collisionLayer.Tiles;
}