我有一個具有枚舉類型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
如果你添加std :: endl到你的印刷品來沖洗這條線有幫助嗎?'std :: cout <<「Text」<< std :: endl;' – 2012-02-05 19:23:29
不,控制檯輸出中仍然沒有任何東西。我已經測試了key_space,不過現在在開關外面,看起來好像按鍵沒有被讀取。這是奇怪的看到,因爲逃生鍵捕獲工作正常。 – 2012-02-05 19:28:37
對不起,我意識到這不在上面的代碼片段。直接在交換機之前,我有另一個theGameEngine-> Key_Hit()作爲退出遊戲的退出鍵。完美地工作! – 2012-02-05 19:32:32