【发布时间】:2014-01-21 21:21:55
【问题描述】:
我正在用 C++ 编写一个蛇游戏,我有一个蛇的一部分的结构,其中包含 x 位置、y 位置、方向等数据。
我已经完成了所有工作,将所有数据设置为整数,我只是想将一些数据类型更改为枚举,因为它看起来更简洁且更易于理解。 我已经尝试了很多并在网上查找,但我似乎找不到任何东西。
这是一些结构:
struct SnakeSection
{
int snakePosX;
int snakePosY;
int SectionType;
// Tail = 0, Body = 1, Head = 2
int animation;
enum Direction
{
Up = 0,
Right = 1,
Down = 2,
Left = 3
};
};
我尝试将其中一个方向传递给另一个函数:
void PlayerSnake::createSnake()
{
// Parameters are direction, x and y pos, the blocks are 32x32
addSection(SnakeSection::Direction::Right, mStartX, mStartY, 2);
}
然后我尝试将方向设置为该函数中传入的方向:
void PlayerSnake::addSection(SnakeSection::Direction dir, int x, int y, int type)
{
//Create a temp variable of a Snake part structure
SnakeSection bufferSnake;
bufferSnake.Direction = dir;
bufferSnake.animation = 0;
//is it head tail or what? This is stored in the Snake section struct
//TODO Add different sprites for each section
bufferSnake.SectionType = type;
//assign the x and y position parameters to the snake section struct buffer
bufferSnake.snakePosX = x;
bufferSnake.snakePosY = y;
//Push the new section to the back of the snake.
lSnake.push_back(bufferSnake);
}
错误:枚举 SnakeSection::Direction 的使用无效
谢谢
【问题讨论】:
-
:-/ Hmmpf,他们最近写了这么多蛇游戏,谁能告诉教授/老师这是一个愚蠢的课堂项目...
标签: c++ data-structures enums