我的类中无法访问公共变量
我正在使用 XNA 框架制作我的第一个游戏。在它的主游戏类 (Breakout.cs
) 中,我将其放入:
public int screenHeight;
public int screenWidth;
在 Initialize
方法中:
this.screenHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
this.screenWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
但是当我尝试从我的 Paddle
访问它时code> class 我收到此错误:
The name 'screenWidth' does not exist in the current context
我一直认为将变量设置为 public 可以使其在任何地方都可访问?
感谢帮助,谢谢。
I am using the XNA framework to make one of my first games. In it's main game class (Breakout.cs
), I put this:
public int screenHeight;
public int screenWidth;
And in the Initialize
method:
this.screenHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
this.screenWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
But when I try and access it from my Paddle
class I get this error:
The name 'screenWidth' does not exist in the current context
I always thought that setting a variable to public would make it accessible any where?
Help is appreciated, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
public
使该字段可以从其他类访问,但您仍然需要对该实例的引用。在Paddle
类中的某个位置应该存在对Breakout
的引用,因为您需要它。例如,通过在实例化Paddle
时分配一个名为“breakout”的Breakout
类型字段。然后,您可以从Paddle
调用breakout.screenHeight
。另一种选择是使
Breakout
成为单例(您还会拥有更多吗?)。声明一个静态字段并在构造时将 Breakout 实例存储在其中。然后公开一个Instance
属性:然后从
Paddle
:不确定是否希望
instance
在 intiailizer 中实例化Breakout
因为我不确定您的游戏类当前是如何实例化的。public
makes the field accessible from other classes, but you still need a reference to the instance. Somewhere in classPaddle
there should exist a reference back toBreakout
since you need it. For example, by having a field of typeBreakout
called "breakout" that you assign when you instantiate aPaddle
. Then, fromPaddle
, you may callbreakout.screenHeight
.Another option is to make
Breakout
a singleton (are you ever going to have more?). Declare a static field and store your instance of Breakout in there when it's constructed. Then expose anInstance
property:Then from
Paddle
:Not certain if you want
instance
to instantiateBreakout
in the intiailizer since I'm not sure how your game class is currently being instantiated.我不熟悉 XNA(尽管我一直想研究它),但听起来您正试图从另一个类中引用一个类的成员。为此,您必须实际引用第一类。您说 screenHeight 和 screenWidth 是在主游戏类中声明的,但随后您说您试图在 Paddle 类中访问它们。但如果你使用“this”,你只能访问 Paddle 的成员,而不能访问“主游戏”类的成员,无论它叫什么。如果您想查看主游戏类的成员,您需要参考一些对主游戏类的引用(而不是“this”)。
示例:
错误:
更好:
I'm not familiar with XNA (though I've been meaning to look into it), but it sounds like you're trying to refer to members of one class from within another class. In order to do that, you have to actually refer to the first class. You said screenHeight and screenWidth are declared in the main game class, but then you said you were trying to access them in the Paddle class. But if you use "this" you can only access members of Paddle, not members of the "main game" class, whatever that's called. You need to refer to some reference to the main game class (instead of "this") if you want to see the members of it.
Examples:
WRONG:
BETTER:
您需要创建 Breakout 类的实例。
然后您可以访问该对象的公共字段。
You need to create an instance of the class Breakout.
Then you can access that object's public fields.
让它成为公共静态
...
公共静态 int screenWidth;
然后只需编写 paddle.maxY = Breakout.screenWidth;
make it a public static
soooo...
public static int screenWidth;
and then just write paddle.maxY = Breakout.screenWidth;