0

クラスのメソッドの戻り値の結果に、新しいリスト変数を次のように設定しようとしています。

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    public static SpriteBatch spriteBatch;

    // ...stuff...

    // Class initialization
    private Map map = new Map() { Width = 10, Height = 10, TileWidth = 16, TileHeight = 16 };

    // Variable declaration
    List<Vector2> blockPos = new List<Vector2>();
    blockPos = map.generateMap(); // Doesn't work

    Texture2D dirtTex;

    // ...more stuff...
}

メソッド内になく、 Update() メソッドに入れることはできるが、それは各フレームで実行され、これを一度だけ実行したいので、機能しないと思います。

上記のコードを実行しようとすると、次の 3 つのエラーが発生します。

'Deep.Game1.blockPos' is a 'field' but is used like a 'type'
Invalid token '=' in a class, struct, or interface member declaration
'Deep.Game1.map' is a 'field' but is used like a 'type'

マップ クラス:

class Map
{
    // Variable declaration
    public int Width { get; set; } // Width of map in tiles
    public int Height { get; set; } // Height of map in tiles
    public int TileWidth { get; set; }
    public int TileHeight { get; set; }
    Random rnd = new Random();

    // Generate a list of Vector2 positions for blocks
    public List<Vector2> generateMap()
    {
        List<Vector2> blockLocations = new List<Vector2>();

        // For each tile in the map...
        for (int w = 0; w < Width; w++)
        {
            for (int h = 0; h < Height; h++)
            {
                // ...decide whether or not to place a tile...
                if (rnd.Next(0, 1) == 1)
                {
                    // ...and if so, add a tile at that location.
                    blockLocations.Add(new Vector2(w * TileWidth, h * TileHeight));
                }
            }
        }

        return blockLocations;
    }
}

コンストラクターを使用してみましたが、エラーが発生していないにもかかわらず、コンストラクター内のコードが実行されていないようです。

public void getPos()
{
    blockPos = map.generateMap();
}
4

1 に答える 1