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.
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.
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
}
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.
A checklist of what worked for me:
Build Action
is set to Compile
.spritefont
file (use this question as a reference) LoadContent
or later in the life-cycle 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.