Hi all,
Currently I am making somewhat of a map using a grid. I got a grid working with extended and fusing fill rectangle, which worked.
Now I made a very basic sprite as a texture for the grid so I can alter the map to make roads/plants/objects etc.
I got the sprite loading and functioning in the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MonoGame.Extended;
using MonoGame.Extended.ViewportAdapters;
namespace VikingFarmer
{
class Map
{
private int TileWidth;
private int TileHeight;
private int Width;
private int Height;
public Animation grass;
public OrthographicCamera camera;
private GraphicsDevice Graphics;
public void Initialize(GameWindow window, GraphicsDevice graphics, int tileWidth, int tileHeight, int width, int height, Texture2D grassTexture )
{
TileHeight = tileHeight;
TileWidth = tileWidth;
Width = width;
Height = height;
Graphics = graphics;
var viewPortAdapter = new BoxingViewportAdapter(window, graphics, 1600, 900) ;
camera = new OrthographicCamera(viewPortAdapter);
grass = new Animation();
Vector2 position = Vector2.Zero;
grass.Initialize(grassTexture, position, 16, 16, 1, 100, Color.White, 1f, true, 0);
}
public void Draw(SpriteBatch spriteBatch)
{
for (int i = 0; i < Width; i++)
{
for (int j = 0; j < Height; j++)
{
grass.Position.X = i * TileWidth;
grass.Position.Y = j * TileHeight;
grass.Draw(spriteBatch);
}
}
}
public void Update(GameTime gameTime, KeyboardState keyboardState, float playerMoveSpeed)
{
//keyboard movement
if (keyboardState.IsKeyDown(Keys.A) || keyboardState.IsKeyDown(Keys.Left))
{
camera.Move(new Vector2(-1, 0));
}
if (keyboardState.IsKeyDown(Keys.W) || keyboardState.IsKeyDown(Keys.Up))
{
camera.Move(new Vector2(0, -1));
}
if (keyboardState.IsKeyDown(Keys.D) || keyboardState.IsKeyDown(Keys.Right))
{
camera.Move(new Vector2(1, 0));
}
if (keyboardState.IsKeyDown(Keys.S) || keyboardState.IsKeyDown(Keys.Down))
{
camera.Move(new Vector2(0, 1));
}
for (int i = 0; i < Width; i++)
{
for (int j = 0; j < Height; j++)
{
grass.Position.X = i * TileWidth;
grass.Position.Y = j * TileHeight;
grass.Update(gameTime);
}
}
}
}
}
However, this does something which is pretty obvious namely drawing one instance of a sprite and updating it over the different positions leaving the last position as a single sprite instance, I knew this was gonna happen but was just to test the sprite. Therefore I made a multi-dimensional array to have multiple instances of the sprite and with it a grid that I can modify to my wishes.
The code resulted in this:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MonoGame.Extended;
using MonoGame.Extended.ViewportAdapters;
namespace VikingFarmer
{
class Map
{
private int TileWidth;
private int TileHeight;
private int Width;
private int Height;
public Animation[,] grass;
public OrthographicCamera camera;
private GraphicsDevice Graphics;
public void Initialize(GameWindow window, GraphicsDevice graphics, int tileWidth, int tileHeight, int width, int height, Texture2D grassTexture )
{
TileHeight = tileHeight;
TileWidth = tileWidth;
Width = width;
Height = height;
Graphics = graphics;
var viewPortAdapter = new BoxingViewportAdapter(window, graphics, 1600, 900) ;
camera = new OrthographicCamera(viewPortAdapter);
grass = new Animation[Width, Height];
Vector2 position = Vector2.Zero;
for (int i = 0; i < Width; i++)
{
for (int j = 0; j< Height; j++)
{
position.X = i * TileWidth;
position.Y = j * TileHeight;
grass[i,j].Initialize(grassTexture,position,16,16,1,100,Color.White,1f,true,0);
}
}
}
public void Draw(SpriteBatch spriteBatch)
{
for (int i = 0; i < Width; i++)
{
for (int j = 0; j < Height; j++)
{
grass[i, j].Draw(spriteBatch);
}
}
}
public void Update(GameTime gameTime, KeyboardState keyboardState, float playerMoveSpeed)
{
//keyboard movement
if (keyboardState.IsKeyDown(Keys.A) || keyboardState.IsKeyDown(Keys.Left))
{
camera.Move(new Vector2(-1, 0));
}
if (keyboardState.IsKeyDown(Keys.W) || keyboardState.IsKeyDown(Keys.Up))
{
camera.Move(new Vector2(0, -1));
}
if (keyboardState.IsKeyDown(Keys.D) || keyboardState.IsKeyDown(Keys.Right))
{
camera.Move(new Vector2(1, 0));
}
if (keyboardState.IsKeyDown(Keys.S) || keyboardState.IsKeyDown(Keys.Down))
{
camera.Move(new Vector2(0, 1));
}
for (int i = 0; i < Width; i++)
{
for (int j = 0; j < Height; j++)
{
grass[i, j].Update(gameTime);
}
}
}
}
}
The issue I know have is that when I run the code I get the error: System.NullReferenceException: Object Reference not set to an instance of an object.
From what I understand this has to do with something not being initialized, however what is the thing that hasnt been initialized then? Since the code with a single instance works.
I am a beginner in programming and am trying to teach myself methods of debugging and understanding error codes and so on, so far I have managed pretty nicely (with one other exception with key input, but that was a good learning experience in coding logic)
However, with this code I couldnt find ways to trial and error to find the issue, does anybody have tips how to find problems with these kind of error codes (other then google, which got me to the conclusion something wasn’t initialized)
Thanks a lot for tips, you guys rock