Attempting to draw from another class not working

So I’m attempting to call a draw method from another class called Start.cs in my Game1.cs but I keep getting this error:

Here’s the code in the Game1.cs:

SpriteBatch spriteBatch;
Texture2D texture;

public Main()
{
	graphics = new GraphicsDeviceManager(this);
	graphics.PreferredBackBufferWidth = 1024;
	graphics.PreferredBackBufferHeight = 576;
	Content.RootDirectory = "Content";
}

protected override void Initialize()
{
	base.Initialize();
}

protected override void LoadContent()
{
	start.LoadContent();
}

protected override void UnloadContent()
{
}

protected override void Update(GameTime gameTime)
{
}

protected override void Draw(GameTime gameTime)
{
	GraphicsDevice.Clear(Color.CornflowerBlue);
	start.Draw(spriteBatch, texture);
}

Here’s the code in Start.cs:

SpriteBatch spriteBatch;
Texture2D texture;

public void LoadContent()
{
	spriteBatch = new SpriteBatch(GraphicsDevice);
	texture = Content.Load<Texture2D>("Art/Background/titleimg");
}

public void Draw(SpriteBatch spriteBatch, Texture2D texture)
{
	spriteBatch.Begin();
	spriteBatch.Draw(texture, new Rectangle(0, 0, 1024, 576), Color.White);
	spriteBatch.End();
}

I’ve tried making a graphicsDevice as a method parameter but that didn’t work, and I couldn’t find a solution to the problem after searching for a bit. Some help would be appreciated, I imagine it’s something simple I keep overlooking.

Thanks.

Where does the GraphicsDevice in your start method come from?

It’s inherited from the Game class correct? I have attempted to make a GraphicsDevice variable before which told me that it cannot be null when creating new resources.

Edited it to clarify it a bit per jjags comment.

You can do it one of four ways.

  1. Pass the game and content manager creating the spritebatch in that class. (long story short not a good idea for the same reason you’re graphicsdevice isn’t valid by default in you’re class)

  2. Do the above but create them in game1 first then pass that and store the references to a static class that you can call anywere instead of start.
    Then in start or anywere else you can call to the static class references i ussually do that cause its simple.

  3. Same as two but just pass the stuff directly to you’re class from game1 either to use directly or pass and save the references and use them in the class.

  4. Use a GameComponent its basically passing game1 around via passing (this) i don’t use the component though it might be simpler for you link at the bottom.

.
.
Since what you are trying to do looks most like option 3

In Game1 just pass the graphics device the initialized spriteBatch and the content manager.

protected override void LoadContent()
{
    spriteBatch = new SpriteBatch(GraphicsDevice);
    start.LoadContent(GraphicsDevice, spriteBatch, Content);
}

protected override void Draw(GameTime gameTime)
{
    start.Draw(gameTime);
    base.Draw(gameTime);
}

In your start class.

You need to add the using directives same as game 1 to the very top of the class file.

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

Then add the following un-instantiated object references into the start class.

public GraphicsDevice = device;
public SpriteBatch spriteBatch;
public Microsoft.Xna.Framework.Content.ContentManager Content;
public Texture2D texture;

In you’re loadcontent method in start.cs you need to make the following changes.

protected void LoadContent(GraphicsDevice device, SpriteBatch spriteBatch, Microsoft.Xna.Framework.Content.ContentManager Content)
{
    this.device = device;
    this.spriteBatch = spriteBatch;
    this.Content = Content;
    // now you can load with content here.
    texture = Content.Load<Texture2D>("Art/Background/titleimg");
}

As well as start.draw

public void Draw(GameTime gameTime)
{
    device.Clear(Color.CornflowerBlue);
    spriteBatch.Begin();
    spriteBatch.Draw(texture, new Rectangle(0, 0, 1024, 576), Color.White);
    spriteBatch.End();
}

here is a overview for a game component.

http://gneu.org/2012/04/xna-components-better-structure-and-organization-2/

1 Like

You should only have 1 class inheriting from Game. Instead of letting Start inherit from game, you can pass the GraphicsDevice from Main to it.

1 Like

Thanks for the help guys. I was going to create a spritebatch in each class and pass the game/content manager in multiple classes so this is a definite improvement over my original idea. Also it was my bad for inheriting from the game class in multiple times.