0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Is that an error made during copying or you provided the arguments to tile constructor in wrong order? In addition, have you checked when creating board the texture is loaded, shouldn't it be in different stage of initialization e.g. LoadContent method? \$\endgroup\$ – wondra Mar 30 '17 at 14:10
  • \$\begingroup\$ Sorry, just tried that and the first argument is still returning null.Thank's though. In addition, I've edited the post with a list of things I've tried. \$\endgroup\$ – DoggoH Mar 30 '17 at 14:24
  • \$\begingroup\$ Fixed! See edit 2 for details, thanks to everyone who took their time to view. Sorry for any inconveniences, still new to all this business. \$\endgroup\$ – DoggoH Mar 30 '17 at 14:32
  • \$\begingroup\$ Much as I expected - it was some order issue. You can also consider posting it as answer to own question rather than edit (best including some research/citation from official or other reliable source that covers the correct order of initialization). \$\endgroup\$ – wondra Mar 30 '17 at 15:45
  • 1
    \$\begingroup\$ Yes, please post what you needed to do as an actual answer. \$\endgroup\$ – Josh Mar 30 '17 at 16:00
-1
\$\begingroup\$

Fixed it! I just had to put mainBoard AFTER the loading the floor and wall graphics in LoadContent.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.