列表突然变空

A list suddenly becomes empty

我正在弄清楚 XNA 是如何工作的,我正在试验一款基于图块的游戏。现在,当我注册磁贴时,我将它们存储在一个列表中,但是当 class 加载程序调用该列表时,它是空的。

瓷砖处理程序

public class Tile
{
    public class TileType
    {
        public int ID;
        public Texture2D Texture;
        public Rectangle Rectangle;
    }

    private List<Handler.Tile.TileType> tiles = new List<Handler.Tile.TileType>();

    public List<Handler.Tile.TileType> Tiles
    {
        get { return tiles; }
    }

    int TileNumber = 0;

    Game1 game = new Game1();


    public void LoadTile(int ID, SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(tiles[ID].Texture, tiles[ID].Rectangle, Color.White);
    }

    public void AddTile(Texture2D texture, Rectangle rectangle, SpriteBatch spriteBatch)
    {
        tiles.Add(new TileType { ID = TileNumber, Texture = texture, Rectangle = rectangle });
        TileNumber++;
        Console.WriteLine("[Tile.cs] - Number of tiles in tiles: " + tiles.Count);
        Console.WriteLine("[Tile.cs] - Added a tile, with ID of " + tiles[TileNumber-1].ID);
    }

    public void LoadMap(SpriteBatch spriteBatch)
    {
        foreach (TileType tile in tiles)
        {
            LoadTile(tile.ID, spriteBatch);
            Console.WriteLine("Loaded Tile: " + tile.ID + "With Texture: " + tile.Texture);
        }
    }
 }

这会添加 Tiles:

class CreateMap
{
    public void createMap(int size, SpriteBatch spriteBatch, List<Texture2D> texture)
    {
        Tile tile = new Tile();
        Random rnd = new Random();
        int air = 1;
        switch(size)
        {
            case 1:
                size = -64;
                break;
            case 2:
                size = -128;
                break;
            case 3:
                size = -256;
                break;
            default:
                size = -64;
                break;
        }
        for (int x = size; x <= (size * -1); x++)
            for (int y = 0; y < 65; y++)
            {
                if (air <= 3)
                {
                    tile.AddTile(texture[1], new Rectangle(x * 64, y * 64, 64, 64), spriteBatch);
                    air++;
                }
                else
                {
                    switch (rnd.Next(0, 3))
                    {
                        case 1:
                            tile.AddTile(texture[1], new Rectangle(x * 64, y * 64, 64, 64), spriteBatch);
                            Console.WriteLine("Added Tile with texture of Dirt");
                            break;
                        case 2:
                            tile.AddTile(texture[0], new Rectangle(x * 64, y * 64, 64, 64), spriteBatch);
                            Console.WriteLine("Added Tile with texture of Sky");
                            break;
                    }
                }
                Console.WriteLine("[ CreateMap.cs ] - Tile Number: " + tile.Tiles.Count);
            }
    }
}

还有 Game1.cs

    public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Handler.Tile tile;
    Handler.CreateMap mapHandler;
    Handler.TextureHandler textureHandler;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    protected override void Initialize()
    {
        tile = new Handler.Tile();
        mapHandler = new Handler.CreateMap();
        textureHandler = new Handler.TextureHandler();
        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        textureHandler.LoadTextures(Content);
        mapHandler.createMap(1, spriteBatch, textureHandler.Textures);
    }

    protected override void UnloadContent()
    {
    }

    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();
        Console.WriteLine("[Game1.cs / Update ] - Tiles in Update: " + tile.Tiles.Count);

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();
        tile.LoadMap(spriteBatch);
        spriteBatch.End();

        base.Draw(gameTime);
    }
}

(更好)说明:当调用 LoadMap() 时,Console.WriteLine() return 为 0,而 CreateMap return 为正确的数字。

您需要 return 您在

中创建的 Tile 对象
class CreateMap
{
    public Tile createMap(int size, SpriteBatch spriteBatch, List<Texture2D> texture)
    {
        Tile tile = new Tile();

        // do the work

        return tile;
    }
}

这需要 returned 到 Game1

public class Game1 : Microsoft.Xna.Framework.Game
{
    // ...

    protected override void Initialize()
    {
        mapHandler = new Handler.CreateMap();
        tile = mapHandler.createMap();
        textureHandler = new Handler.TextureHandler();
        base.Initialize();
    }
}