first time on here. So I'm trying to make a tile based game in Monogame using some 2D arrays and spriteBatch.draw absolutely refuses to retrieve a Texture2D from an array, or something else is wrong with my code which I'm sure is the case.
Here's some of the samples. Excuse the ugliness, still kind of beginning MonoGame.
Tile class:
class Tile {
#region Init
private Texture2D _gfx;
private Vector2 _pos;
private BoundingBox _bb = new BoundingBox();
public Texture2D Graphic {
get {
return _gfx;
}set {
_gfx = value;
}
}
public Vector2 Position {
get {
return _pos;
}
set {
_pos = value;
}
}
#endregion
#region Constructors
public Tile() { } //Empty constructor
public Tile(Vector2 position) {
Position = position;
}
public Tile(Texture2D graphic) {
Graphic = graphic;
}
public Tile(Texture2D graphic, Vector2 position) {
Position = position;
Graphic = graphic;
}
#endregion
}
TileManager Class:
class TileManager {
#region Methods
public static Tile[,] createBoard(int width, int height, Texture2D graphic, float offset) {
Tile[,] tArray = new Tile[width, height];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
tArray[x, y] = new Tile(new Vector2(x * offset, y * offset), graphic);
}
}
return tArray;
}
public static Tile getTileAt(Tile[,] tile_array, int x, int y) {
return tile_array[x, y];
}
#endregion
}
and of course the whole main class:
public class Game1 : Game {
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D floor;
Texture2D wall;
Color myColor;
Tile newTile;
Texture2D newTexture;
Tile[,] mainBoard;
public Game1() {
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize() {
// TODO: Add your initialization logic here
mainBoard = TileManager.createBoard(10, 10, floor, 25f);
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent() {
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
floor = Content.Load<Texture2D>("floor");
wall = Content.Load<Texture2D>("wall");
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// game-specific content.
/// </summary>
protected override void UnloadContent() {
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime) {
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime) {
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
for (int x = 0; x < 10; x++) {
for (int y = 0; y < 10; y++) {
spriteBatch.Draw(mainBoard[x, y].Graphic, mainBoard[x, y].Position, Color.White);
// Fist argument is null no matter what?
}
}
spriteBatch.End();
base.Draw(gameTime);
}
}
Thanks in advance.
Edit: Here are some things I tried
-Making content "Copy if newer" and "Always Copy"
-Put "mainBoard" in loadContent
-using Tiles Graphic only constructor in tileManager
Edit2:
Fixed it! I just had to put mainBoard AFTER the loading the floor and wall graphics in LoadContent.
LoadContent
method? \$\endgroup\$