我的类中无法访问公共变量

发布于 2024-09-26 18:50:13 字数 577 浏览 8 评论 0原文

我正在使用 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

残疾 2024-10-03 18:50:13

我一直认为将变量设置为 public 可以使其在任何地方都可以访问?

public 使该字段可以从其他类访问,但您仍然需要对该实例的引用。在 Paddle 类中的某个位置应该存在对 Breakout 的引用,因为您需要它。例如,通过在实例化 Paddle 时分配一个名为“breakout”的 Breakout 类型字段。然后,您可以从 Paddle 调用 breakout.screenHeight

另一种选择是使 Breakout 成为单例(您还会拥有更多吗?)。声明一个静态字段并在构造时将 Breakout 实例存储在其中。然后公开一个 Instance 属性:

private static Breakout instance = new Breakout();

public static Breakout Instance 
{
    get { return instance; }
}

然后从 Paddle

Breakout.Instance.screenHeight

不确定是否希望 instance 在 intiailizer 中实例化 Breakout因为我不确定您的游戏类当前是如何实例化的。

I always thought that setting a variable to public would make it accessible anywhere?

public makes the field accessible from other classes, but you still need a reference to the instance. Somewhere in class Paddle there should exist a reference back to Breakout since you need it. For example, by having a field of type Breakout called "breakout" that you assign when you instantiate a Paddle. Then, from Paddle, you may call breakout.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 an Instance property:

private static Breakout instance = new Breakout();

public static Breakout Instance 
{
    get { return instance; }
}

Then from Paddle:

Breakout.Instance.screenHeight

Not certain if you want instance to instantiate Breakout in the intiailizer since I'm not sure how your game class is currently being instantiated.

遗忘曾经 2024-10-03 18:50:13

我不熟悉 XNA(尽管我一直想研究它),但听起来您正试图从另一个类中引用一个类的成员。为此,您必须实际引用第一类。您说 screenHeight 和 screenWidth 是在主游戏类中声明的,但随后您说您试图在 Paddle 类中访问它们。但如果你使用“this”,你只能访问 Paddle 的成员,而不能访问“主游戏”类的成员,无论它叫什么。如果您想查看主游戏类的成员,您需要参考一些对主游戏类的引用(而不是“this”)。

示例:

错误:

class MyGame
{
  public int screenWidth;
  public int screenHeight;
  void MainLoop()
  {
     Paddle p = new Paddle();
  }
}

class Paddle
{
  void Initialize()
  {
     int w = screenWidth;
     int h = screenHeight;
  }
}

更好:

class MyGame
{
  public int screenWidth;
  public int screenHeight;
  void MainLoop()
  {
     Paddle p = new Paddle(this);
  }
}

class Paddle
{
  MyGame game;

  public Paddle(MyGame game)
  {
     this.game = game;
  }

  void Initialize()
  {
     int w = game.screenWidth;
     int h = game.screenHeight;
  }
}

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:

class MyGame
{
  public int screenWidth;
  public int screenHeight;
  void MainLoop()
  {
     Paddle p = new Paddle();
  }
}

class Paddle
{
  void Initialize()
  {
     int w = screenWidth;
     int h = screenHeight;
  }
}

BETTER:

class MyGame
{
  public int screenWidth;
  public int screenHeight;
  void MainLoop()
  {
     Paddle p = new Paddle(this);
  }
}

class Paddle
{
  MyGame game;

  public Paddle(MyGame game)
  {
     this.game = game;
  }

  void Initialize()
  {
     int w = game.screenWidth;
     int h = game.screenHeight;
  }
}
优雅的叶子 2024-10-03 18:50:13

您需要创建 Breakout 类的实例。

Breakout breakout = new Breakout();

然后您可以访问该对象的公共字段。

int height = breakout.screenHeight;

You need to create an instance of the class Breakout.

Breakout breakout = new Breakout();

Then you can access that object's public fields.

int height = breakout.screenHeight;
晨光如昨 2024-10-03 18:50:13

让它成为公共静态

...

公共静态 int screenWidth;

然后只需编写 paddle.maxY = Breakout.screenWidth;

make it a public static

soooo...

public static int screenWidth;

and then just write paddle.maxY = Breakout.screenWidth;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文