在遊戲更新循環中,將bool標誌從false更改爲true的最佳方式是什麼?例如,如果我在下面有這個簡單的例子,如果按住按鈕「A」,Input類將Game類的啓用布爾值設置爲true,如果你釋放它,則將其設置爲false,並將Game類中的計數器設置爲false啓用次數從真變爲假。例如,如果按「A」並釋放兩次計數器應該更新爲2.在60fps更新Game :: Update()時,計數器將與當前方法錯誤。爲了解決這個問題,我將SetEnable中的檢查和計數器移到了Update循環中。在遊戲更新循環中計數bool真/假更改
// Input class
// Waits for input
void Input::HandleKeyDown()
{
// Checks if key A is pressed down
if (key == KEY_A)
game.SetEnable(true);
}
void Input::HandleKeyUp()
{
// Checks if key A is released
if (key == KEY_A)
game.SetEnable(false);
}
// Game class
void Game::SetEnable(bool enable)
{
if(enable == enable_)
return;
enable_ = enable;
//Will increment the counter as many times A was pressed
if(enable)
counter_ += 1;
}
void Game::Update()
{
// Updates with 60fps
// Will increment the counter as long as A is pressed
/*
if(enable_ == true)
counter_ += 1;
*/
}
更新在您更改狀態的情況下櫃檯。所以如果按下KEY_A,並且enable_爲false,則更改爲true並計數。 –
計數器並不真正記錄啓用更改的次數。 –
由於問題不明確,我不得不改寫和添加更多細節。讓我知道是否需要進一步更新。 – sabotage3d