我在一個正在處理的項目的類中有一個委託出現問題。該類是一個同時接受標籤和值的GUI組件。這裏的想法是,用戶可以指定一個標籤,然後從任意位置鏈接一個值(更具體地說,該值的ToString方法),以便每次更新該值時,GUI組件也是如此。這是它是如何設置的基礎知識:代表細節
public delegate string GUIValue();
public class GUIComponent
{
GUIValue value = null; // The value linked in
string label = ""; // The label for the value
string text = ""; // The label and value appended together
public GUIComponent(string Text, GUIValue Value)
{
this.text = Text;
this.value += Value;
}
public void Update()
{
this.text = this.label + this.value();
}
}
然後我這樣稱呼它
GUIComponent component = new GUIComponent("Label: ",
new GUIValue(this.attribute.ToString));
的代碼編譯正確,和組件會顯示,並顯示爲初始值給它的屬性,但是,只要屬性值改變,它就不會更新。
我的問題是我是否有這個設置的權利,如果是的話,爲什麼它不會工作。我最初的想法是它只接受ToString方法返回的第一個值,因爲它沒有任何參數,但任何人都可以驗證?
你什麼時候調用`Update`方法? – Justin 2011-02-04 18:36:50
每幀更新方法由遊戲代碼自動調用。 – shmeeps 2011-02-04 18:38:13