Я только что начал использовать события и делегаты в C #, и я не уверен, что именно так мне следует настроить.
MenuScreen.cs
public class MenuScreen {
public class MenuScreen {
public List <MenuEntry> _entries = new List<MenuEntry> {};
public void Initialize () {
foreach (MenuEntry entry in _entries) {
entry.EntrySelected += onEntrySelected;
}
}
private void onEntrySelected (object sender, MenuEntryEventArgs e) {
Debug.WriteLine (e._text);
}
}
MenuEntry.cs
public class MenuEntry {
public delegate void EventHandler (object sender, MenuEntryEventArgs e);
public event EventHandler EntrySelected;
private ButtonState _clickstate = ButtonState.Released;//Left mouse button state
public void Draw (MenuScreen menuscreen, Vector2 position) {
if (Mouse.GetState ().LeftButton == ButtonState.Pressed) {//If left mouse button is pressed
if (_clickstate == ButtonState.Released && bounds.Contains (x, y)) {//If left mouse button isn't already pressed and mouse is in entry hitbox
onEntrySelected ();
}
_clickstate = ButtonState.Pressed;//Left mouse button state is pressed
} else {//If left mouse button is released
_clickstate = ButtonState.Released;//Left mouse button state is released
}
protected virtual void onEntrySelected () {
EventHandler handler = EntrySelected;
MenuEntryEventArgs args = new MenuEntryEventArgs (_text);
if (handler != null) {
handler (this, args);
}
}
}
Полагаю, это должно быть лучше?Я посмотрел на ветку, на которую он разместил ссылку, и использовал ее в качестве руководства.