2013-03-27 42 views
1

基本上我試圖在我的遊戲中實現一個保存函數。當您單擊菜單中的保存時,遊戲將寫入.txt文件。從MenuManager類我想內Game1.cs從另一個類調用函數-XNA 4.0-C#

調用一個函數,這是我MenuManager.cs代碼

   case Menu.ButtonEvents.Save: 
        activeMenu.ButtonEvent = Menu.ButtonEvents.None; 
        game1.Save(); 
        Show("Save Menu"); 

第三行是我要調用的函數Game1.cs

在Game1.cs我有保存功能

public void Save() 
    { 
     // Example #1: Write an array of strings to a file. 
     // Create a string array that consists of three lines. 
     string[] lines = { levelIndex.ToString(), player.checkpointPos.X.ToString(), player.checkpointPos.Y.ToString() }; 
     System.IO.File.WriteAllLines("WriteText.txt", lines); 
    } 

,我的問題是,我都在Game1.cs的變量,但保存按鈕是在我內按下NuManager系統。任何幫助將不勝感激。

+0

然後,菜單管理器需要訪問遊戲對象。它是一個'GameComponent'嗎? – 2013-03-27 16:01:35

+1

從Game1中暴露遊戲對象的屬性,然後從您的經理類中調用'Game.Save()'etc .. – DGibbs 2013-03-27 16:03:30

+1

'Expose'是什麼意思? – Brecon 2013-03-27 18:55:12

回答

0

您需要將對game1變量的引用傳遞給您的MenuManager類。

嘗試這樣:

MenuManager

public Game1 Game 
{ 
    get { return this._game; } 
    set { this._game = value; } 
} 
private Game1 _game = null; 

Game1.cs

this.MenuManager.Game = this; 

在你的函數:

case Menu.ButtonEvents.Save: 
activeMenu.ButtonEvent = Menu.ButtonEvents.None; 
this.Game.Save(); 
Show("Save Menu");