Reputation: 13
I have read many different solutions to problems similar to this but I cannot find a solution that works for me.
I am just starting on making a simple game to learn the basics of XNA, but I cannot get textures to load in an additional class. I tried this:
EDIT: This is not the main class incase I didn't make that clear enough
class Wizard
{
// Variables
Texture2D wizardTexture;
GraphicsDeviceManager graphics; // I added this line in later, but it didn't seem to do anything
public Wizard(ContentManager content, GraphicsDeviceManager graphics)
{
this.graphics = graphics;
Content.RootDirectory = "Content";
LoadContent();
}
protected override void LoadContent()
{
wizardTexture = Content.Load<Texture2D>("Wizard"); // Error is here
base.LoadContent();
}
I have also tried making a method like
public Texture2D Load(ContentManager Content)
{
return Content.Load<Texture2D>("Wizard");
}
And then have wizardTexture = Load(Content); but that did not work either.
Any help and an explanation is appreciated, thanks
Upvotes: 0
Views: 3240
Reputation: 16854
Works in this way
texture = Game.Content.Load<Texture2D>(textureName);
Upvotes: 0
Reputation: 5762
That's not the usual constructor for an xna game... it seems that you are using a hack to let using the game class in a winform... if you want to use that way... you are passing wrong the parameteres or your not creating right the graphicsdevicemanager
The usual way to create a xna game is defining this two files:
// program.cs file
static class Program
{
static void Main(string[] args)
{
using (Game1 game = new Game1())
{
game.Run();
}
}
}
// Game1.cs file
public class Game1 : Microsoft.Xna.Framework.Game {
GraphicsDeviceManager graphics;
public Game1( ) {
graphics = new GraphicsDeviceManager( this );
Content.RootDirectory = "Content";
}
....
}
You should realize that the game constructor has no parameters, and the graphicsdevicemanager its created inside the constructor
EDIT: I was thinking that maybe your wizard class its not intended to be a game, but a GameComponent or DrawableGameComponent, in this case it should be:
class Wizard : DrawableGameComponent {
Texture2D wizardTexture;
public Wizard(Game game) : base(game)
{
}
protected override void LoadContent()
{
wizardTexture = Content.Load<Texture2D>("Wizard"); // Error is here
base.LoadContent();
}
....
}
Then in you main game class when you initialize the object... you can add add it to the Components collection.
class Game1: Game {
....
public override void Initialize() {
Components.Add( new Wizard(this));
}
}
Upvotes: 1