我在我的更新循環中調用以下方法來檢查是否按下了其中一個按鈕。調用方法的順序 - 爲什麼在這個例子中很重要?
private void CheckInputs()
{
if (CheckButton(startNewGameButtonTexture, startNewGameButtonPosition))
{
//break 1 is here
}
if (CheckButton(continueCareerButtonTexture, continueCareerButtonPosition))
{
//break 2 is here
}
}
按鈕只是矩形和鼠標是否超過他們,並點擊被釋放CheckButton布爾返回true:
public bool CheckButton(Texture2D texture, Vector2 vector)
{
MouseState CurrentMouseState = Mouse.GetState();
bool outcome;
if (CurrentMouseState.X > vector.X && CurrentMouseState.X < vector.X + texture.Width &&
CurrentMouseState.Y > vector.Y && CurrentMouseState.Y < vector.Y + texture.Height)
{
if (CurrentMouseState.LeftButton == ButtonState.Released && PreviousMouseState == ButtonState.Pressed)
{
outcome = true;
}
else
{
outcome = false;
}
}
else
{
outcome = false;
}
PreviousMouseState = CurrentMouseState.LeftButton;
return outcome;
}
在目前的順序,startNewGameButton有效(即調試站在休息1),但continueCareerButton不起作用(點擊按鈕不會觸發休息2)。
但是,如果我改變的檢查順序是:
private void CheckInputs()
{
if (CheckButton(continueCareerButtonTexture, continueCareerButtonPosition))
{
//break 2 is here
}
if (CheckButton(startNewGameButtonTexture, startNewGameButtonPosition))
{
//break 1 is here
}
}
的continueCareerButton現在工程(突破200),但現在startNewGameButton不(突破100)。
我認爲鼠標狀態一定不能正常工作,但我無法解決原因。
Doh!謝謝15char – user2056166
沒問題:)我已經在過去自己做過..:\ –