简体   繁体   English

如何从其他类中访问我的“当前屏幕”变量?

[英]How do I access my “Current Screen” variable from within other classes?

I have a bunch of screens that all inherit from a base abstract class called GameScreen. 我有一堆屏幕都从一个名为GameScreen的基础抽象类继承。 They include various buttons that, if pressed, I want to use to change the Current Screen. 它们包括各种按钮,如果按下,我想用来改变当前屏幕。 For now, I am trying to change the screen from startScreen to overviewScreen. 目前,我正在尝试将屏幕从startScreen更改为overviewScreen。

I have a variable in my Game1 class: 我的Game1类中有一个变量:

public GameScreen CurrentScreen;

Which is initially set as: 最初设置为:

CurrentScreen = startScreen;

I then use the following lines to Update and Draw the game, based on which screen is the Current screen: 然后我根据哪个屏幕是当前屏幕使用以下行来更新和绘制游戏:

CurrentScreen.Update();
...
CurrentScreen.Draw(spriteBatch);

In my startScreen classs, I want to write something like this within the update method: 在我的startScreen类中,我想在update方法中编写类似的东西:

if (//Button is pressed)
{
     game.CurrentScreen = overviewScreen; 
}

Now clearly that won't work. 现在显然不会奏效。 But I can't see how to do it. 但我看不出怎么做。 Basically I want to access the CurrentScreen variable from within a class, and change it to whichever screen I want, and I feel like there must be a clean way to do this. 基本上我想从一个类中访问CurrentScreen变量,并将其更改为我想要的任何一个屏幕,我觉得必须有一个干净的方法来做到这一点。

Let me know if any additional info is required, I feel like I haven't explained this at all well. 如果需要任何其他信息,请告诉我,我觉得我没有完全解释这一点。

EDIT 编辑

startScreen and overviewScreen are classes that, primarily contain the Update and Draw methods for screens I want to display. startScreen和overviewScreen是主要包含我想要显示的屏幕的Update和Draw方法的类。 GameScreen is a base class they all derive from. GameScreen是他们都来自的基类。 CurrentScreen is just meant to be a variable that determines which screen is active. CurrentScreen只是一个变量,用于确定哪个屏幕处于活动状态。

use the static objects 使用静态对象

public static GameScreen CurrentScreen;

and same for the overviewScreen 和overviewScreen相同

Ok, notw it's cleaner for me. 好吧,对我来说它更清洁。 You could add few methods to your Game1 class 您可以为Game1类添加一些方法

public void SwitchToStartScreen(){...}
public void SwitchToOverviewScreen(){...}
//etc

Then you could add a property to your startScreen class (and other ones too) 然后你可以添加一个属性到你的startScreen类(以及其他类)

public Game1 Parent {get;set;}

And force the constructor to pass Game1 object, in which you would assign that parameter to Parent property. 并强制构造函数传递Game1对象,在该对象中,您将该参数分配给Parent属性。 Then on particular condition you could just use 然后在特定条件下你可以使用

Parent.SwitchToOverviewScreen();

Accessing and changing properties manually from outer classes is violating object-oriented principles. 从外部类手动访问和更改属性违反了面向对象的原则。

Also, use brief naming convention, because it's hard to understand what you wrote if one of class definitions uses Pascal case while other use Camel case. 此外,使用简短的命名约定,因为如果其中一个类定义使用Pascal案例而其他类使用Camel案例,则很难理解您所编写的内容。 Your startScreen has more like an object name, while it's used like a class name. 你的startScreen更像是一个对象名,而它的使用就像一个类名。 Eithar that or you're not seeing the difference between class and an object. Eithar,或者你没有看到阶级和对象之间的区别。 By default in C# Pascal case is used for declaring classes. 默认情况下,在C#中,Pascal case用于声明类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我无法从我的应用程序中的其他类访问App.xaml.cs? - Why do I not have access to App.xaml.cs from other classes in my app? 如何从标记页面访问变量? - How do I access a variable from my markup page? 我如何从多个其他类中访问一个类中的一些方法 - how do I give access to a few methods in one class from many other classes 如何在自定义任务中访问当前项目上下文? - How do I access current Project context within a custom Task? 如何从其他类访问私有成员? - How can I access private members from other classes? 如何从同一 class C# 中的另一种方法访问一种方法的变量? - How do I access the variable of one method from another method within the same class C#? 从我的控制器操作中,我可以访问HttpContextBase吗? - From within my controllers action, do I have access to HttpContextBase? 如何从站点内的其他页面访问控件? - How do I access a control from a different page within my site? 如何从.Net Core 2.1的静态函数中访问我的配置? - How do I access my configuration from within a static function in .Net Core 2.1? 如何在控制器中访问POST请求的参数 - How do I access parameters of my POST request within a controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM