2014-01-21 55 views
1

我正在用C++編寫一個Snake遊戲,我有一段蛇的結構,它包含諸如x位置,y位置,方向等數據將一個結構的枚舉傳遞給其他函數並賦值

我把它全部工作,把所有的數據設置爲整數,我只是想將一些數據類型改爲enum,因爲它看起來更整齊,更容易理解。 我試了很多,看上去在線,但我似乎無法找到任何東西。

這是一些結構:

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 ::方向

感謝

+0

: -/Hmmpf,他們最近寫了這麼多的蛇遊戲,誰能告訴教授/教師這是一個愚蠢的課程項目... –

回答

0

以下行的錯誤...

bufferSnake.Direction = dir; 

...是合理的,除了那個聲明enum類型,你仍然必須有一個類的成員變量其存儲:

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 
    }; 

    Direction direction_; // <<<<<<<<<<<<<< THAT'S WHAT'S MISSING IN YOUR CODE 
}; 

,並參考

bufferSnake.direction_= dir; // <<<<<<<<<<<<<< THAT'S THE MEMBER VARIABLE YOU'LL 
          //    HAVE TO REFER TO! 
+0

很酷的感謝,因爲 – shineSparkz