Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Player class with my C# XNA game

Tags:

c#

class

xna

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.

like image 914
Wen Avatar asked Oct 08 '10 19:10

Wen


2 Answers

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.

like image 55
Andrew Russell Avatar answered Oct 04 '22 02:10

Andrew Russell


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
    }
like image 41
Michael Todd Avatar answered Oct 04 '22 04:10

Michael Todd