5

I would usually seek help on the XNA forums but they're down at the moment, so I've come here.

I am making a new XNA game, and I want to have a player class. At the moment I have a main game class called Dots. This represents the main game. This is how my Player class is laid out at the moment:

namespace Dots
{
    class Player : Microsoft.Xna.Framework.Game
    {
        Texture2D PlayerTexture;
        Vector2 PlayerPosition;

        public Player()
        {
            Content.RootDirectory = "Content";
            PlayerTexture = Content.Load<Texture2D>("Player");
            PlayerPosition = Vector2.Zero;
        }

        public void Update()
        {

        }

        public void Draw(SpriteBatch SpriteBatch)
        {

        }
    }
}

But I'm getting an error which I can't work out how to solve. The error is:

Error loading "Player". GraphicsDevice component not found.

It's throwing it at this line: PlayerTexture = Content.Load<Texture2D>("Player");.

I see in the main game class there is this line: Graphics = new GraphicsDeviceManager(this); but I have no idea what to do with it. Do I pass it to my Player class, or what?

Any help is appreciated, thanks.

4
  • Not familiar with XNA, but this post implies that you shouldn't load content in a constructor. Oct 8, 2010 at 19:53
  • Arg. THen I have no idea how to properly do what I want to do. I'll have to wait until the XNA forums are open again and take a look how they reccomend doing it.
    – Wen
    Oct 8, 2010 at 20:07
  • Why not ask here? XNA programmers are... um programmers... that's what StackOverflow is for.
    – WernerCD
    Oct 8, 2010 at 20:10
  • Sorry, I didn't mean I'd HAVE to wait until their forums are up. But it might be better asking there because their help forum is, after all, specificially for the XNA framework.
    – Wen
    Oct 8, 2010 at 20:23

5 Answers 5

6

First of all you seem to lack an understanding of how classes (and namespaces) work. I'd suggest (as I have also done in a comment on your other question) working on a simple game that doesn't need classes, first.

Now to talk about the problems in your code:

First of all, in order to load content, the graphics device needs to have been initialized. It is not initialized until LoadContent is called in Microsoft.Xna.Framework.Game. This is explained in the documentation:

This method is called by Initialize. Also, it is called any time the game content needs to be reloaded, such as when the DeviceReset event occurs. You should not access the GraphicsDevice until LoadContent is called.

So move your loading code:

protected override void LoadContent()
{
    PlayerTexture = Content.Load<Texture2D>("Player");
    PlayerPosition = Vector2.Zero;
    base.LoadContent();
}

Notice how it is an override (of a protected method). I don't really want to get into an explanation of what this means here, but I suggest you find out.

Additionally, you will find that Draw and Update are supposed to be similarly overriden, as they will be called from XNA.

Now, here's the important bit: What I've just told you applies if you are inheriting your game class from the XNA Game class. By calling it "Player" you are indicating a misunderstanding of how classes work.

If you are making a "Player" class, take a look at what you were told in the answer to your other question.

You may also find this recent question worth reading.

2

UPDATE: This answer is apparently correct for older versions of XNA. See Andrew Russell's answer for what's being used currently.

Try overriding the LoadGraphicsContent method to load your content. As shown in this tutorial.

    protected override void LoadGraphicsContent(bool loadAllContent)
    {
        if (loadAllContent)
        {
            spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
            // TODO: Load any ResourceManagementMode.Automatic content
            t2dMap = content.Load<Texture2D>(@"content\textures\map_display");
            t2dColorKey = content.Load<Texture2D>(@"content\textures\map_colorkey");
        }
        // TODO: Load any ResourceManagementMode.Manual content
    }
2
  • LoadGraphicsContent has been obsolete for a very long time. Oct 9, 2010 at 16:19
  • 1
    @Andrew Russell: Thanks, Andrew. I was trying to help the OP along even though I'm unfamiliar with XNA. I'll leave this answer here in case someone else stumbles across it. That, along with your comment and answer to this question, should help steer people in the right direction in the future. Oct 9, 2010 at 20:06
1

Recently in XNA, you can now right click on certain options and have Intellisense fix it for you. So, by right clicking on Texture2D and Rectangle when you are declaring you should be able to choose "Resolve" and it will ask you to choose "using" some other library so that it can compile then. See if that fixes your problem. But, generally with classes especially I use this and it works well.

0

A checklist of what worked for me:

  • Try putting the font in the root folder of your content project
  • Make sure the Build Action is set to Compile
  • Verify the contents of the .spritefont file (use this question as a reference)
  • Make sure you're in LoadContent or later in the life-cycle
-2

Think I had a similar error once.

Right click on the Solution in the Solution Explorer.

Go to Properties.

If Game Profile is "Hi Def".. change to "Reach".

Recompile and Run.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.