我有一个名为的文件夹StartUp
,它包含开始的类,并且首先在程序中加载。我想访问另一个名为的文件夹OverAll
,其中包含将在整个游戏中使用的类。我想从OverAll
一个名为的类中学习Mouses
,然后将其转移到中的任何其他类上StartUp
。
编辑: 10 : 00 12/7/2013
这是“文件和文件夹”,以Mouses.cs
具有的示例为例,public int thing = 1;
我希望它移至TitleScreen.cs
,我可以TitleScreen.cs
通过什么方式查看/编辑该int?
我可以Game1.cs
通过调用那里的文件夹然后访问类(例如OverAll.Mouses.ect = 1;
)和vic-versa来访问所有类。
OverAll.Mouses.cs
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Lover__Regret
{
class Mouses
{
public int thing = 1;
}
}
StartUp.TitleScreen.cs
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Love__Regret.StartUp
{
class TitleScreen
{
public static void Update(GameTime gameTime)
{
//Want to call thing here
}
}
}
好吧,其中的thing
字段Mouses
不是静态字段。这意味着您需要对Mouses
inside有一个实例引用TitleScreen
,以便能够对其进行访问。
您说Game1.cs
您已经在引用实例。因此,我建议执行以下操作:
Update
在方法TitleScreen
应该不会是静态的。您需要更新特定TitleScreen
实例。因此,删除static关键字。为此创建一个构造函数,TitleScreen
该构造函数接受的一个实例,Mouses
并使用一个字段来存储该实例:
专用Mouses _mouseHandler;
public TitleScreen(Mouses mouseHandler){_mouseHandler = mouseHandler; }
然后,在Update
方法中调用如下内容:
Console.WriteLine(_mouseHandler.thing); //或您想对来自以下字段的任何操作Mouses
现在,当您TitleScreen
在中构建时Game1.cs
,像这样创建它,并传递一个Mouses
实例:
//创建mouses实例(例如:Mouses
mouses = new Mouses())TitleScreen屏幕= new TitleScreen(鼠标);
大多数编程不是通过静态类/方法完成的(这将是过程式编程)。相反,您需要创建类的实例。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句