我目前有一个学校项目,我正在制作游戏。这是我的代码:
public partial class room2 : Form
{
public room2()
{
InitializeComponent();
Random rand1 = new Random();
Int32 t =6 ;
Int32 Fight = rand1.Next(1, 11);
Int32 J = 10;
label4.Text = Convert.ToString(10);
if (Fight <= t)
{
label3.Text = Convert.ToString(J);
}
else
{
txtDialogBox.Text = ("No Fight in this room");
}
}
private void Attack_Click(object sender, EventArgs e)
{
Random rand2 = new Random();
Int32 J = 10;
Int32 Attack = rand2.Next(1, 4);
Int64 y = 1;
Int64 t = 10;
//opponents hp bar goes down by 1
J --;
label3.Text = Convert.ToString(J);
// chance for your hp bar to go down
if (Attack >= y)
{
label4.Text = Convert.ToString(t);
t--;
}
}
}
当我把Ints放在顶部时(就像我被告知的那样)我得到错误(&#34;在当前环境中不存在&#34;)我发现我解决它的唯一方法是把它放在按钮上。
答案 0 :(得分:2)
在方法(或构造函数,在您的情况下)中声明的局部变量只能由方法本身访问。
在C#中,开放括号( {和} )定义了块。
每个块都有自己的范围。在块中定义变量时,您将在块的范围内定义它。
现在,这是最重要的部分,块继承父块的范围,但无法访问子块的范围或任何其他外部块。
例如,
public void SomeMethod() { //This is the method's scope
int someVar = 10; //We define a variable on the method's scope
//We can access "someVar" within the method
if(someVar < 10) { //This is the if scope. It inherits the parent method scope.
someVar = someVar + 1; //Can access parent's scope
bool valid = true; //We define a variable on the if's scope
}
valid = false; //We can't do this. "valid" was defined in a child scope (if).
//We can only access our scope or parent's scope.
}
此外,该方法无法访问其他方法的变量,因为它们是外部范围。
这就是你发生的事情。
您正试图从int
事件处理程序中的其他方法访问Click
个变量。您需要定义那些变量全局(在类的范围内,也就是父范围),或者在方法的本地范围内定义新变量。
我希望自己足够清楚。
答案 1 :(得分:0)
你必须取消一些变量并将它们全局声明以供所有方法使用。
应该看起来像这样
public partial class Form1 : Form
{
Int32 t = 6;
Int32 J = 10;
public Form1()
{
InitializeComponent();
Random rand1 = new Random();
Int32 Fight = rand1.Next(1, 11);
label4.Text = Convert.ToString(J);
if (Fight <= t)
label3.Text = Convert.ToString(J);
else
txtDialogBox.Text = ("No Fight in this room");
}
private void Attack_Click(object sender, EventArgs e)
{
Random rand2 = new Random();
Int32 Attack = rand2.Next(1, 4);
Int64 y = 1;
//opponents hp bar goes down by 1
J--;
label3.Text = Convert.ToString(J);
// chance for your hp bar to go down
if (Attack >= y)
{
label4.Text = Convert.ToString(t);
t--;
}
}
}
答案 2 :(得分:0)
koko,
您可能知道,room2是您所在的表单类的构造函数。(因为您使用:运算符实现表单)
您可以通过设置这些变量访问修饰符来在构造函数外部声明int。
try:
public partial class room2 : Form
{
private int32 J=10;
private Int64 y = 1;
private Int64 t = 10;
private Int32 J = 10;
private Int32 t = 6 ;
//This would mean J is a private variable that only members of this class can access
//This is because it is declared at the top of the class (like you said, as you were advised to do)
//If you declare this in the constructor, other class elements cannot access it.
//OTHER Variable code here then:
public room2()
{
InitializeComponent();
Random rand1 = new Random();
Int32 Fight = rand1.Next(1, 11);
label4.Text = Convert.ToString(10);
if (Fight <= t)
{
label3.Text = Convert.ToString(J);
}
else
{
txtDialogBox.Text = ("No Fight in this room");
}
}
private void Attack_Click(object sender, EventArgs e)
{
Random rand2 = new Random();
Int32 J = 10;
Int32 Attack = rand2.Next(1, 4);
//opponents hp bar goes down by 1
J --;
label3.Text = Convert.ToString(J);
// chance for your hp bar to go down
if (Attack >= y)
{
label4.Text = Convert.ToString(t);
t--;
}
}
//Additionally, just for learning
public int32 J=10
//would mean anything, even things out of this class can access code in the class.
protected int32 J=10
//would mean anything that *Implements* this has access to this variable (something called an assembly)
这样,类知道这是这个类的私有变量,任何类函数都可以使用。 了解访问修饰符并找到有效的修饰符。通常,将所有类变量设为私有是安全的,并使用公共访问函数来修改它们。