我正在制作这个游戏而且我收到了错误。 “在将控制权返回给调用者之前,必须完全分配字段”。我似乎无法弄清楚这一点,它让我疯狂。这是我的代码
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);
}
}
}
}
}
}
}
答案 0 :(得分:0)
这意味着你结构中的所有字段都应该在构造函数
中初始化您忘记在Block
结构
private Block[,] grid;
private string filename;
public Point playerStartaPos;
但我建议你在这种情况下使用一个类 When to use struct?
答案 1 :(得分:0)
我知道现在已经有一周了,但我想我应该给我两分钱。
您获得这些错误的原因是因为在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 ++,但仍然值得一看!)
这是C#
最后,Youtube上有一个名为&#39;的教程。 3D游戏引擎教程&#39;通过BennyBox,他将带您踏上创建自己的3D游戏引擎的旅程,甚至为您提供每个教程的代码。一定要看看。
祝你有美好的一天!