必须完全分配OpenTK块

时间:2016-11-25 13:12:23

标签: c# opentk

您好我使用的是c#和opentk,我有这段代码

using System;
using System.Drawing;
using System.IO;
using OpenTK;


namespace PlatformGame
{
    struct Level
    {
        private Block[,] grid;
        public Block this[int X, int Y]
        {
            get
            {
                return grid[X, Y];
            }
            set
            {
                grid[X, Y] = value;
            }
        }

        public Point PlayerStartPos;

        private string filename;
        public string FileName
        {
            get
            {
                return filename;
            }
        }


        public int Width
        {
            get
            {
                return grid.GetLength(0);
            }
        }
        public int Height
        {
            get
            {
                return grid.GetLength(1);
            }
        }

        public Level(int Width, int Height)
        {
            grid = new Block[Width, Height];
            filename = "none";
            PlayerStartPos = 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.Water, x, y);
                    }
                    else
                    {
                        grid[x, y] = new Block(BlockType.Empty, x, y);
                    }
                }
            }
        }
    }

    public enum BlockType
    {
        Empty,
        Water,
        Grass,
        Dirt,
        Stone,
        Platform,
        Ladder,
        LadderPlatform
    }

    struct Block
    {
        private BlockType type;

        private int PosX, PosY;

        private bool Water, Grass, Dirt, Stone, Platform, Ladder, LadderPlatform;

        public BlockType Type
        {
            get { return type; }
        }

        public int X
        {
            get { return PosX; }
        }
        public int Y
        {
            get { return PosY; }
        }
        public bool IsWater
        {
            get { return Water; }
        }
        public bool IsGrass
        {
            get { return Grass; }
        }
        public bool IsDirt
        {
            get { return Dirt; }
        }
        public bool IsStone
        {
            get { return Stone; }
        }
        public bool IsPlatform
        {
            get { return Platform; }
        }
        public bool IsLadder
        {
            get { return Ladder; }
        }

        public Block(BlockType type,int x,int y)
        {
            this.PosX = x;
            this.PosY = y;
            this.type = type;

            this.Water = false;
            this.Grass = false;
            this.Dirt = false;
            this.Stone = false;
            this.Ladder = false;
            this.Platform = false;

            switch (type)
            {
                case BlockType.Ladder:
                    Ladder = true;
                    break;
                case BlockType.LadderPlatform:
                    Ladder = true;
                    Platform = true;
                    break;
                case BlockType.Platform:
                    Platform = true;
                    break;
                case BlockType.Water:
                    Water = true;
                    break;
                case BlockType.Grass:
                    Grass = true;
                    break;
                case BlockType.Dirt:
                    Dirt = true;
                    break;
                case BlockType.Stone:
                    Stone = true;
                    break;
            }
        }

    }
}

由于某些原因,visual studio在public Block

给我这个错误
  

Field&#39; Block.LadderPlatform&#39;在将控制权返回给调用者PlatformGame

之前必须完全分配

我试图理解这个问题,但我无法解决它。

1 个答案:

答案 0 :(得分:1)

您在初始化时错过了this.LadderPlatform = false;

如果您在switch public Block(BlockType type,int x,int y) 语句之前添加此内容,则可以使用

像这样:

public Block(BlockType type, int x, int y)
{
    this.PosX = x;
    this.PosY = y;
    this.type = type;

    this.Water = false;
    this.Grass = false;
    this.Dirt = false;
    this.Stone = false;
    this.Ladder = false;
    this.Platform = false;
    this.LadderPlatform = false;  //<--- This is missing

    switch (type)
    {
        case BlockType.Ladder:
            Ladder = true;
            break;
        case BlockType.LadderPlatform:
            Ladder = true;
            Platform = true;
            break;
        case BlockType.Platform:
            Platform = true;
            break;
        case BlockType.Water:
            Water = true;
            break;
        case BlockType.Grass:
            Grass = true;
            break;
        case BlockType.Dirt:
            Dirt = true;
            break;
        case BlockType.Stone:
            Stone = true;
            break;
        default:
            break;
    }
}