我正在制作一个简单的流浪者游戏,其中我遇到一个我无法想象的简单问题
这是我的主要功能,它制作一个流动站对象,其中流动站可以处理多个设备,有一个设备提取样本
Rover rov = new Rover();
string input;
//Device d = new Device();
//Specimen spec = new Specimen();
Console.WriteLine("Welcome to Planetary Rover");
Console.WriteLine("Enter y to start the game or n to exit");
string selection = Console.ReadLine();
if (selection == "y")
{
Console.WriteLine("Enter the rover Default Location X: ");
rov.X =
do
{
Console.WriteLine("Write the device nname to operate ");
input = Console.ReadLine();
rov.Handle(input);
//d.Operate();
} while (input != "end");
}
if (selection == "n")
{
Console.Clear();
}
else
{
Console.WriteLine("Wrong Input!");
}
钻头是在试样操作时将试样外加到试样上的装置,只有当试样x和y等于转向器x和y时才会提取 我已经完成了这些操作,但是在我的钻类中有操作函数,它正在执行上述操作,但是每当钻孔操作时都会出现问题,它再次制作一个新的流动站,所以流浪者x和y在那个时间变为空,所以任何都可以给出我可以解决这个问题,如何在钻孔功能中使用现有的流动站
public override void Operate(string ids)
{
Rover rov = new Rover();
if (specim.X == rov.X && specim.Y == rov.Y)
{
_wearfactor += 5;
Console.WriteLine("specimen extracted and wearfaction of drill is incresed by 5%");
Console.WriteLine(_wearfactor);
_spec. = 0;
}
if(specim.X != rov.X && specim.Y != rov.Y)
{
_wearfactor += 10;
Console.WriteLine("wear factor increased by 10%");
Console.WriteLine(_wearfactor);
}
if (_wearfactor == 100)
{
Console.WriteLine("Drill Destroyed");
Console.WriteLine("syart your program again");
Console.WriteLine("You can not drill specimen now");
this.Name = "";
}
}
答案 0 :(得分:0)
您可以将操作方法的签名更改为:
public override void Operate(Rover rov, string ids)
然后当您创建Rover rov = new Rover();
时,您可以将其传递给Operate
以供使用。
Operate(rov, "ids");
另一个选择是创建一个公共Rover对象并通过Operate
直接引用它:
// assuming your main class is MainFunction
public class MainFunction()
{
public Rover rov { get; private set; }
public static void Main(string[] args)
{
// Establish the reusable rover
rov = new Rover();
// ...
}
}
然后在Operate
中,将所有rov
更改为MainFunction.rov