c#字段必须完全赋值错误

本文关键字:赋值 错误 字段 | 更新日期: 2023-09-27 18:18:24

我正在做这个游戏,我得到了错误。"在将控制权返回给调用方之前,必须完全分配字段"。我似乎就是弄不明白,它快把我逼疯了。这是我的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using System.Drawing;
using System.IO;
namespace Box2
{
struct Level
{
    private Block[,] grid;
    public int Height
    {
        get {
            return grid.GetLength(1);
        }
    }
    public int Width {
        get
        {
            return grid.GetLength(0);
        }
    }
    public enum BlockType
    {
        Solid,
        Empty,
        Platform,
        Ladder,
        LadderPlatform
    }
      struct Block
    {
        private BlockType type;
        private int posX, posY;
        private bool solid, platform, ladder;
        public BlockType Type
        {
            get { return type; }
        }
        public int X
        {
            get { return posX; }
        }
        public int Y
        {
            get { return posY; }
        }
        public bool IsSolid
        {
            get { return solid; }
        }
        public bool IsPlatform
        {
            get { return platform; }
        }
        public bool IsLadder
        {
            get { return ladder; }
        }
        private Block[,] grid;
        private string filename;
        public Point playerStartaPos;
        public Block(BlockType type, int x, int y)
        {
            this.posX = x;
            this.posY = y;
            this.type = type;
            this.ladder = false;
            this.solid = false;
            this.platform = false;
            switch (type)
            {
                case BlockType.Ladder:
                    ladder = true;
                    break;
                case BlockType.LadderPlatform:
                    ladder = true;
                    platform = true;
                    break;
                case BlockType.Solid:
                    solid = true;
                    break;
                case BlockType.Platform:
                    platform = true;
                    break;
                default:
                    break;
            }
        }

        public Block this[int x, int y]
        {
            get
            {
                return grid[x, y];
            }
            set
            {
                grid[x, y] = value;
            }
        }
        public string FileName
        {
            get { return filename; }
        }
        public void Level (int width, int height)
        {
            grid = new Block[width, height];
            filename = "none";
            playerStartaPos = new Point(1,1);
            for (int x=0; x < width; x++)
            {
                for (int y =0; y< height; y++)
                {
                    if (x == 0 || y ==0 || x ==width -1 || y == height -1)
                    {
                        grid[x, y] = new Block(BlockType.Solid, x, y);
                    }
                    else
                    {
                        grid[x, y] = new Box2.Level.Block(BlockType.Empty, x, y);
                    }
                }
            }
        }
    }
}

}

c#字段必须完全赋值错误

这意味着你的结构中的所有字段都应该在你的构造函数中初始化

你忘了在你的Block结构体

中初始化这些字段
        private Block[,] grid;
        private string filename;
        public Point playerStartaPos;

,但我建议您在这种情况下使用类什么时候使用结构体?

我知道这篇文章已经发布一周了,但我想我应该提出我的意见。

你得到这些错误的原因是因为当你在c#中创建一个结构时,你必须初始化构造函数中的所有字段。我不能100%确定你对c#的了解程度,所以我冒昧地为你修改了你的代码。请注意,事情可以写得更整洁一些,你应该努力实现漂亮的代码结构。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.IO;
namespace Box2
{
    struct Level
    {
        private Block[,] grid;
        public int Height
        {
            get
            {
                return grid.GetLength(1);
            }
        }
        public int Width
        {
            get
            {
                return grid.GetLength(0);
            }
        }
        public enum BlockType
        {
            Solid,
            Empty,
            Platform,
            Ladder,
            LadderPlatform
        }
        struct Block
        {
            private BlockType type;
            private int posX, posY;
            private bool solid, platform, ladder;
            public BlockType Type
            {
                get { return type; }
            }
            public int X
            {
                get { return posX; }
            }
            public int Y
            {
                get { return posY; }
            }
            public bool IsSolid
            {
                get { return solid; }
            }
            public bool IsPlatform
            {
                get { return platform; }
            }
            public bool IsLadder
            {
                get { return ladder; }
            }
            private Block[,] grid;
            private string filename;
            public Point playerStartaPos;
            public Block(BlockType type, int x, int y)
            {
                this.grid = null;
                this.filename = null;
                this.playerStartaPos = Point.Empty;
                this.posX = x;
                this.posY = y;
                this.type = type;
                this.ladder = false;
                this.solid = false;
                this.platform = false;
                switch (type)
                {
                    case BlockType.Ladder:
                        ladder = true;
                        break;
                    case BlockType.LadderPlatform:
                        ladder = true;
                        platform = true;
                        break;
                    case BlockType.Solid:
                        solid = true;
                        break;
                    case BlockType.Platform:
                        platform = true;
                        break;
                    default:
                        break;
                }
            }

            public Block this[int x, int y]
            {
                get
                {
                    return grid[x, y];
                }
                set
                {
                    grid[x, y] = value;
                }
            }
            public string FileName
            {
                get { return filename; }
            }
            public void Level(int width, int height)
            {
                grid = new Block[width, height];
                filename = "none";
                playerStartaPos = new Point(1, 1);
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        if (x == 0 || y == 0 || x == width - 1 || y == height - 1)
                        {
                            grid[x, y] = new Block(BlockType.Solid, x, y);
                        }
                        else
                        {
                            grid[x, y] = new Box2.Level.Block(BlockType.Empty, x, y);
                        }
                    }
                }
            }
        }
    }
}

如果这有助于你解决你的问题,请记住通过点击我的答案左边的勾号来接受我的答案。

还有,最后一件事。我注意到这段代码来自Youtube上的一个平台教程,我只想说,如果你热衷于用OpenTK学习c#,这些是迄今为止你能得到的最好的参考:

(这是c++,但仍然值得深入研究!)

学习OpenGL

这个是c#

学习OpenTK

最后,在Youtube上有一个名为"3D游戏引擎教程"的教程,由BennyBox,他带你踏上创造你自己的3D游戏引擎的旅程,甚至为你提供每个教程的代码。一定要去看看。

祝你有美好的一天!