Reputation: 173
Recently started working with XNA (coming from java) and run into a problem with displaying game screens. When loading up XNA I get given a game.cs class which I interpreted to be a set a functions for drawing a single self-contained screen in the game. As obviously typing the code for all your different screens into this single class would get very messy very quickly so I created the below class to handle changes:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Colonies
{
public class GameManager //manages game screens
{
Microsoft.Xna.Framework.Game currentScreen;
public enum screens { menu, game, summary };
public GameManager()
{
initialize();
}
public void initialize()
{
currentScreen = new Menu(this);
((Menu)currentScreen).Run();
}
public void changeScreen(int i)
{
switch (i)
{
case 0:
currentScreen = new Menu(this);
((Menu)currentScreen).Run();
break;
case 1:
currentScreen = new World(this);
((World)currentScreen).Run();
break;
case 2:
currentScreen = new Summary(this);
((Summary)currentScreen).Run();
break;
}
}
}
}
However when one of these changes is triggered this causes an error flag up telling me I can't call game run more than once. Does this mean by initial estimation about having a single all purpose game screen is actually correct?! Should be manager instead be being queried for game like screens which methods are then called in the main game.cs class?
i.e.
in game.cs update method for example:
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
aGameManager.getAGameObject.DoAnUpdate();
base.Update(gameTime);
}
So essentially my main game class is never run again but just changes what it displays. Would this be the correct solution? (So much of the game class is hidden I am not sure what is the correct way to use it)
Upvotes: 2
Views: 945
Reputation: 2027
create enumerator
enum gamestate
mainmenu
gameplay
options
end enum
and then simply in your update (draw) main functions
if gamestate = mainmenu then mainmenu.update();
if gamestate = gameplay then gameplay.update()
Upvotes: 0
Reputation: 1377
The Game
class is the entire game. That's why it's called Game
. If you want, you can create 'screen' objects, that each control a different screen, and use the Game
class as you were trying to use the 'game manager'.
EG:
public static int currentScreen = 0; // Any screen can change this variable when needed
List<Screenobject> myscreens = new List<Screenobject>(); // Populate this with screens
// OR
menuscreen = new menuScreen();
otherscreen = new otherScreen();
// ...
protected override void Update(GameTime gameTime)
{
myscreens[currentScreen].Update(gameTime);
// OR
switch (currentScreen)
{
case 1:
menuscreen.Update(gameTime); break;
// ...
}
base.Update(gameTime);
}
and Draw(..)
the same as Update(..)
Upvotes: 1