1

I want to create instances of the class Tile, what I am planning to do with a for-Loop. For containing the Tiles I use an Array, which I've declared in the Main Game Class :

Tile[] Tiles;

The creation of the instances follows in a Method, because necessary graphics need to be loaded:

protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        splashText = Content.Load<Texture2D>("SplashText");
        splashRect = new Rectangle((int)center.X - splashText.Width / 2, (int)center.Y - splashText.Height / 2, splashText.Width, splashText.Height);
        gras = Content.Load<Texture2D>("Gras");
        border = Content.Load<Texture2D>("Border");
        borderHighlight = Content.Load<Texture2D>("BorderHighlighted");
        emerald = Content.Load<Texture2D>("Emerald");

        // TODO: use this.Content to load your game content here

        Tiles[0] = new Tile (new Vector2(920, 540), gras);          //The Error refferes to this line
        /*Tile tile0 = new Tile(new Vector2(920, 540), emerald);
        Tile tile1 = new Tile(new Vector2(920, 476), gras);
        Tile tile2 = new Tile(new Vector2(856, 508), gras);
        Tile tile3 = new Tile(new Vector2(792, 540), gras);
        Tile tile4 = new Tile(new Vector2(856, 572), gras);
        Tile tile5 = new Tile(new Vector2(920, 604), gras);
        Tile tile6 = new Tile(new Vector2(984, 572), gras);
        Tile tile7 = new Tile(new Vector2(1048, 540), gras);
        Tile tile8 = new Tile(new Vector2(984, 508), gras);
        Tiles = new Tile[] { tile0, tile1,  tile2, tile3, tile4, tile5, tile6, tile7, tile8 };*/
    }

I don't want to use the code in comments, because I don't know how to name the instances in a for-Loop.

How can I create them the other way without getting the error? Thanks for your support!

Marek Legris
  • 111
  • 1
  • 9

1 Answers1

0

You have declared Tiles as an array of Tile. You haven't instantiated it, or given it a size, however. You're going to want to look into the usage of an Array.

You can also try doing this with a List.

List<Tile> tiles = new List<Tile>();

This creates a List object of type Tile, and instantiates all of the Tiles, using their default constructor.

You can then use:

Tiles.Add(new Tile(new Vector2(920, 540), gras);
Khale_Kitha
  • 274
  • 1
  • 9