0
votes

The question at hand here is whether it is a good idea to have one public SpriteBatch in the Game class, which is then used by all Screens. This would avoid reallocating objects whenever the active Screen changes.

However, I see people using new and private SpriteBatches in each and every Screen. Why do people do this? Am I missing something here?

2

2 Answers

1
votes

Using a single Batch instance is advised, but I don't think public (or static) variables are a good idea, since they break the encapsulation and make it harder to refactor the code. Imagine moving the screen class to another application: you would have to replace all references to the batch in your Game extension or you'd be forced to create such field in yet another project.

Instead, I'd pass the batch in screen constructor. Screens are for displaying views, after all: no need to inform them about the rest of the application.

1
votes

You're right. They should not be doing that. You can make it final if you want to protect it a bit. All objects that use it should know to set the parameters that they need, such as applying their own projection matrix and blend mode.