0

尝试将 CubeDescriptor 类型的变量添加到数组时,我收到错误错误:数组初始值设定项只能在变量或字段初始值设定项中使用。尝试改用新的表达式。我在这个论坛上查看了其他一些主题,但我似乎无法弄清楚我在这里做错了什么。

public class CubeDescriptor
{
    public EcubeType CubeType;
    public Texture2D Texture;
    public bool isMineable;
}

public static CubeDescriptor[] TypeTable = {
                                            {EcubeType.Air, null, false},
                                            {EcubeType.Grass, grass, false},
                                            {EcubeType.Stone, stone, true}
                                           };
4

2 回答 2

2
public class CubeDescriptor
{
    public EcubeType CubeType;
    public Texture2D Texture;
    public bool isMineable;
}

public static CubeDescriptor[] TypeTable = new {
                                               new CubeDescriptor(EcubeType.Air, null, false),
                                               new CubeDescriptor(EcubeType.Grass, grass, false),
                                               new CubeDescriptor(EcubeType.Stone, stone, true)

                                           };

编辑:

如果你没有构造函数,那么你可以做

public static CubeDescriptor[] TypeTable = new {
                                               new CubeDescriptor {CubeType = EcubeType.Air, Texture2D = null, isMineable = false},
                                               new CubeDescriptor {CubeType = EcubeType.Grass, Texture2D = grass, isMineable = false},
                                               new CubeDescriptor {CubeType = EcubeType.Stone, Texture2D = stone, isMineable = true}

                                           };
于 2013-07-17T15:32:29.450 回答
2

C# 不支持 C 样式的结构初始化。

要使用 填充数组CubeDescriptor,您需要调用CubeDescriptor构造函数来创建一个新实例。

您可以使用对象初始值设定项语法设置字段:

new CubeDescriptor { CubeType = ..., Texture = ..., ... }
于 2013-07-17T15:33:15.733 回答