简体   繁体   中英

What is the best way to access fields/properties of the Game1 class in xna?

I'm making an xna game and there is going to be many classes that control the drawing and updating and such.

These classes need to be able to access the instance of the original Game1 class, so they can control the graphics device and sprite batch and stuff.

The problem is that I am going to have a network of classes, classes that call other classes, and the classes at the very bottom of the chain still need to be able to access the Game1 class for the graphics device and such.

What is the best way to do this? Just keep passing along the Game1 as a parameter, or what?

The "correct" answer from a software engineering/architecture perspective is to pass the specific resources you need as parameters into your methods. This provides many advantages - probably the greatest of which is that it decouples the various parts of your code, allowing them to be used independently of each other.

To give you an example: You might have your game. But then you might want to take your same code and make an editor, or unit tests, or any manner of other crazy tools. In many of these cases you might not even have a Game class. (To give you a concrete example, this video of mine shows you two very different ways of running the same game code - this would not be possible without proper decoupling.)

(On this basis, you should probably pass around more specific things than a class derived from Game .)

You can go a step further, if you have fairly open versioning/dependency requirements, and pass around an IServiceProvider (for example, this is what XNA passes to the constructor of ContentManager ). I did a detailed analysis of why you might do this in this answer .

HOWEVER

You are not writing a library that will be used by third parties. You are writing a game. The only people who will be using your code are your own team - and you have access the source code and you can freely modify it.

This means that you are free to take some shortcuts.

In practice it is usually fine to grab a static instance of your game class, or (and this is my preferred method when I am throwing something together) static properties of your game class. And in XNA you know that you will only ever have a single instance of the game class in practice.

It is quick and easy to do this in the first place. And it is also quick and easy to go in later on and switch to doing thing the "right" way. As long as you are prepared for it. Good version control and using "Find All References" in Visual Studio are particularly helpful here.

Basically: as long as you understand that you are taking a shortcut, why you are taking it, and what the "correct" way is - then it is ok to take that shortcut.

为什么不在game1的某个地方保留一个静态引用,一切都是指那个?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM