您的游戏必须具有或能够获得对按钮管理器的引用。通常你的游戏将创建并拥有buttonManager。
class Game
{
ButtonManager m_buttonManager;
...
}
你的按钮管理器可以暴露像OnButtonClicked事件。
class ButtonManager
{
private Button m_playGameButton;
// delegate to define the type of the event handler
public delegate void ButtonClickedEventHandler(ButtonId buttonId);
// event object matching the type of the event handler
public event ButtonClickedEventHandler OnButtonClicked;
void Update()
{
...
if (m_playGameButton.Clicked)
{
// Launch the event when appropriate if there are any subscribers
if (OnButtonClicked != null)
{
OnButtonClicked(ButtonId.PlayGame)
}
}
}
}
您的游戏类可以订阅事件并提供处理程序方法。
class Game
{
...
void Initialise()
{
m_buttonManager += ButtonClicked;
}
void ButtonClicked(ButtonId buttonId)
{
switch (buttonId)
{
case ButtonId.PlayGame:
PlayGame();
break;
case ButtonId.OptionsMenu:
OptionsMenu();
break;
}
}
...
}
或者,游戏类可以轮询按钮管理器。
class Game
{
...
void Update()
{
if (m_buttonManager.IsPlayGameButtonHit)
{
PlayGame();
}
else if (m_buttonManager.IsOptionsMenuButtonHit)
{
OptionsMenu();
}
}
...
}
+1:这就是我的做法,这意味着我可以重新使用我的引擎,而不用硬编码值。 – 2011-01-14 06:58:49