[英]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.