GameplayScreen does not contain a definition for GraphicsDevice
- by Dave Voyles
Long story short: I'm trying to intergrate my game with Microsoft's Game State Management. In doing so I've run into some errors, and the latest one is in the title. I'm not able to display my HUD for the reasons listed above.
Previously, I had much of my code in my Game.cs class, but the GSM has a bit of it in Game1, and most of what you have drawn for the main screen in your GameplayScreen class, and that is what is causing confusion on my part.
I've created an instance of the GameplayScreen class to be used in the HUD class (as you can see below). Before integrating with the GSM however, I created an instance of my Game class, and all worked fine.
It seems that I need to define my graphics device somewhere, but I am not sure of where exactly. I've left some code below to help you understand.
public class GameStateManagementGame : Microsoft.Xna.Framework.Game
{
#region Fields
GraphicsDeviceManager graphics;
ScreenManager screenManager;
// Creates a new intance, which is used in the HUD class
public static Game Instance;
// By preloading any assets used by UI rendering, we avoid framerate glitches
// when they suddenly need to be loaded in the middle of a menu transition.
static readonly string[] preloadAssets =
{
"gradient",
};
#endregion
#region Initialization
/// <summary>
/// The main game constructor.
/// </summary>
public GameStateManagementGame()
{
Content.RootDirectory = "Content";
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 1280;
graphics.PreferredBackBufferHeight = 720;
graphics.IsFullScreen = false;
graphics.ApplyChanges();
// Create the screen manager component.
screenManager = new ScreenManager(this);
Components.Add(screenManager);
// Activate the first screens.
screenManager.AddScreen(new BackgroundScreen(), null);
//screenManager.AddScreen(new MainMenuScreen(), null);
screenManager.AddScreen(new PressStartScreen(), null);
}
namespace Pong
{
public class HUD
{
public void Update(GameTime gameTime)
{
// Used in the Draw method
titleSafeRectangle = new Rectangle
(GameplayScreen.Instance.GraphicsDevice.Viewport.TitleSafeArea.X,
GameplayScreen.Instance.GraphicsDevice.Viewport.TitleSafeArea.Y,
GameplayScreen.Instance.GraphicsDevice.Viewport.TitleSafeArea.Width,
GameplayScreen.Instance.GraphicsDevice.Viewport.TitleSafeArea.Height);
}
}
}
class GameplayScreen : GameScreen
{
#region Fields
ContentManager content;
public static GameStates gamestate;
private GraphicsDeviceManager graphics;
public int screenWidth;
public int screenHeight;
private Texture2D backgroundTexture;
private SpriteBatch spriteBatch;
private Menu menu;
private SpriteFont arial;
private HUD hud;
Animation player;
// Creates a new intance, which is used in the HUD class
public static GameplayScreen Instance;
public GameplayScreen()
{
TransitionOnTime = TimeSpan.FromSeconds(1.5);
TransitionOffTime = TimeSpan.FromSeconds(0.5);
}
protected void Initialize()
{
lastScored = false;
menu = new Menu();
resetTimer = 0;
resetTimerInUse = true;
ball = new Ball(content, new Vector2(screenWidth, screenHeight));
SetUpMulti();
input = new Input();
hud = new HUD();
// Places the powerup animation inside of the surrounding box
// Needs to be cleaned up, instead of using hard pixel values
player = new Animation(content.Load<Texture2D>(@"gfx/powerupSpriteSheet"), new Vector2(103, 44), 64, 64, 4, 5);
// Used by for the Powerups
random = new Random();
vec = new Vector2(100, 50);
vec2 = new Vector2(100, 100);
promptVec = new Vector2(50, 25);
timer = 10000.0f; // Starting value for the cooldown for the powerup timer
timerVector = new Vector2(10, 10);
//JEP - one time creation of powerup objects
playerOnePowerup = new Powerup();
playerOnePowerup.Activated += PowerupActivated;
playerOnePowerup.Deactivated += PowerupDeactivated;
playerTwoPowerup = new Powerup();
playerTwoPowerup.Activated += PowerupActivated;
playerTwoPowerup.Deactivated += PowerupDeactivated;
//JEP - moved from events since these only need set once
activatedVec = new Vector2(100, 125);
deactivatedVec = new Vector2(100, 150);
powerupReady = false;
}