2012-02-05 127 views
2

我有一個具有枚舉類型GameStates的類。在(公共)構造我初始化GameStates這樣的:C++類枚舉成員變量

GameStates enumGameState = Ready; 
在一個公共的方法運行

然後()我有一個這樣的開關:

switch(enumGameState) 
     { 
     case Ready: 
      if (theGameEngine->KeyHit(Key_Space)) 
      { 
       enumGameState = Firing; 
       cout << "\nGame State moved to Firing"; 
      } // End if 
      break; 
     case Firing: 
      if (theGameEngine->KeyHit(Key_Space)) 
      { 
       enumGameState = Contact; 
       cout << "\nGame State moved to Contact"; 
      } // End if 
      break; 
     case Contact: 
      if (theGameEngine->KeyHit(Key_Space)) 
      { 
       enumGameState = Over; 
       cout << "\nGame State moved to Over"; 
      } // End if 
      break; 
     case Over: 
      break; 
     }; // End of GameState switch 

雖然代碼沒有錯誤,沒有國家得到滿足。我應該如何訪問enumGameState的值?

編輯:所有的類代碼。

class Game 
{ 
private: 
    Block* arrBlocks[10][10]; 
    //IMesh* objBlockMesh; 
    IMesh* objGunMesh; 
    IMesh* objDummyMesh; 
    Gun* objGun; 
    int Game::intSpeed; 
    I3DEngine* theGameEngine; 
    float fltSkyboxXCo; 
    float fltSkyboxYCo; 
    float fltSkyboxZCo; 
    float fltFloorXCo; 
    float fltFloorYCo; 
    float fltFloorZCo; 

    enum GameStates{Ready,Firing, Contact, Over}; 
    GameStates enumGameState; 

public: 

    Game(I3DEngine* the3dengine) 
    { 
     Game::theGameEngine = the3dengine; 
     theGameEngine->StartWindowed(); 

     // Add default folder for meshes and other media 
     theGameEngine->AddMediaFolder("C:\\TL-Engine\\Media\\AssigmentTwo\\Media"); 

     //intSpeed = 1; 

     Game::DrawBasicScene(theGameEngine); 
     Game::DrawBlocks(); 
     Game::CreateAGun(); 
     Bullet::Bullet(theGameEngine); 
     Game::enumGameState = Ready; 
    } // End of Constructor 


private:  


    void DrawBlocks() 
    { 
     float fltBlockOffSet = 12.0f; 
     float fltLeftMost = -54.0f; 
     float fltBlockZCo = 120.0f; 
     float fltBlockYCo = 5.0f; 
     float fltCurrentXCo; 
     float fltCurrentYCo = 5.0f; 
     float fltCurrentZCo = 120.0f; 
     // Stick 10 blocks in an array 
     // Display them 

     for(int i = 0; i < 10; i++) 
     { 
      if (i == 1) // Once i have created the first row all the other blocks are going to be created in a hidden state 
      { 
       fltCurrentYCo = -50.0f; 
      } 
      for(int j = 0; j < 10; j++) 
      { 
       fltCurrentXCo = ((float)j*fltBlockOffSet) + fltLeftMost; // Cast j into a float explicitly so that it doesn't it implicitly 
       arrBlocks[i][j] = new Block(theGameEngine, fltCurrentXCo, fltCurrentYCo, fltCurrentZCo); 
       if(fltCurrentYCo < 0) 
       { 
        arrBlocks[i][j]->SetBlockState(Block::Destroyed); 
       } // End if 
       else 
       { 
        arrBlocks[i][j]->SetBlockState(Block::New); 
       } 
      } // End of inner loop 

      fltCurrentZCo += fltBlockOffSet; 

     } // End of outer loop 

    } 

    void CreateAGun() 
    { 
     // Create a gun 
     Gun::Gun(theGameEngine); 
    } 

public: 
    void Game::Run() 
    { 
     //Start watching input in a while loop 
     // The main game loop, repeat until engine is stopped 
     while (theGameEngine->IsRunning()) 
     { 
      // Draw the scene 
      theGameEngine->DrawScene(); 
      if (theGameEngine->KeyHit(Key_Escape)) 
      { 
       theGameEngine->Stop(); 
      } 

      if)theGameEngine->KeyHit(Key_Space)) 
      { 
       cout << "\n space"; 
      } 

      GameStates currentGameState = enumGameState; 
      switch(enumGameState) 
      { 
      case Ready: 
       if (theGameEngine->KeyHit(Key_Space)) 
       { 
        enumGameState = Firing; 
        cout << "\nGame State moved to Firing" << endl; 
       } // End if 
       break; 
      case Firing: 
       if (theGameEngine->KeyHit(Key_Space)) 
       { 
        enumGameState = Contact; 
        cout << "\nGame State moved to Contact" << endl; 
       } // End if 
       break; 
      case Contact: 
       if (theGameEngine->KeyHit(Key_Space)) 
       { 
        enumGameState = Over; 
        cout << "\nGame State moved to Over" << endl; 
       } // End if 
       break; 
      case Over: 
       break; 
      }; // End of GameState switch 
     } 

    } 
}; // End of Game Class 
+0

如果你添加std :: endl到你的印刷品來沖洗這條線有幫助嗎?'std :: cout <<「Text」<< std :: endl;' – 2012-02-05 19:23:29

+0

不,控制檯輸出中仍然沒有任何東西。我已經測試了key_space,不過現在在開關外面,看起來好像按鍵沒有被讀取。這是奇怪的看到,因爲逃生鍵捕獲工作正常。 – 2012-02-05 19:28:37

+0

對不起,我意識到這不在上面的代碼片段。直接在交換機之前,我有另一個theGameEngine-> Key_Hit()作爲退出遊戲的退出鍵。完美地工作! – 2012-02-05 19:32:32

回答

1

感謝您的幫助球員。問題不在於班級,而是班級的代碼。這是一個視覺工作室的問題,我不知道什麼不幸。當代碼被拷貝出來並添加到一個新的項目中時,它編譯得很完美,並且所有放置在main中的斷點都被命中。

故事的道理:嘗試顯而易見的第一個。

4

如果你的構造函數有下面的代碼行,逐字:

GameStates enumGameState = Ready; 

那麼你剛剛做的是你的構造方法中創建本地變量enumGameState並初始化它。一旦構造函數完成,它就超出範圍,並且其值將丟失。

想必你也有一個成員變量enumGameState,其值是未初始化的,所以你switch語句與假值運行。

創建與成員變量名稱相同的局部變量是C++中 shadowing的一個示例,它通常表示出現錯誤。出於這個原因,如果你隱藏一個變量,一些編譯器(比如GCC)會顯示警告;詳情請參閱this answer

+0

GameStates的實例在類的頂部定義爲enumGameState,只在構造函數中設置爲Ready。 – 2012-02-05 19:37:31

+0

@Stephen - 然後我不確定問題是什麼。沒有看到實際的代碼很難說。 – 2012-02-05 19:44:44

+0

大規模失敗,偶然添加了所有代碼,然後嘗試通過編輯帖子將其刪除,並以兩次結束。 :(對不起 PS,可以請一個模去除多餘的代碼呢? – 2012-02-05 19:58:25

1

你才能用它來定義此枚舉:

enum GameState 
{ 
    Ready, 
    Firing, 
    Contact, 
    Over 
}; 

那麼類Game看起來是這樣的:

class Game 
{ 
public: 
    Game(GameState gs = Ready) : gs(gs) { } 
    void update() 
    { 
     switch (gs) 
     { 
     case Ready: cout << "Ready\n"; gs = Firing; break; 
     case Firing: cout << "Firing\n"; gs = Contact; break; 
     case Contact: cout << "Contact\n"; gs = Over; break; 
     case Over: cout << "Over\n"; break; 
     default: break; 
     } 
    } 
private: 
    GameState gs; 
}; 

而這裏的main

int main() 
{ 
    Game g; 
    g.update(); 
    g.update(); 
    g.update(); 
    g.update(); 
    return 0; 
} 

輸出:

Ready 
Firing 
Contact 
Over 
+0

我有一個公共枚舉GameStates定義,已經是它的一個實例。我有一個名爲enumGameState,我這個分配在構造函數中準備值也是公開的,我試圖在方法中使用相同的類變量 – 2012-02-05 19:36:00

+0

好吧,似乎你的實現是正確的,但你決定使用枚舉而不是'int gameState;';) – LihO 2012-02-05 20:03:18

+0

我想我做得正確,但我不知道爲什麼我沒有得到期望的結果。 我真的很希望我在離開前24小時才離開它,現在重新開始我的任務,儘管調試舊的更糟! – 2012-02-05 20:16:04

1

我不相信這個類本身有什麼問題,我發現當我運行遊戲時,main()永遠不會進入,所以這個類永遠不會存在。這讓我陷入了另一個困境,因爲那是遊戲場景的畫面,看起來好像還好。

謝謝你的時間幫助我調試遊戲類的代碼都一樣。