从另一个XNA 4.0 - c#调用函数
本文关键字:调用 函数 另一个 XNA | 更新日期: 2023-09-27 18:06:12
基本上,我正试图在我的游戏中实现保存功能。当你点击菜单中的save时,游戏就会写入一个txt文件。从MenuManager类中,我想调用Game1.cs
中的函数这是我的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中拥有所有变量,但保存按钮是在菜单管理器系统中按下的。
您需要将对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");