Figuring out how to handle content in xna 4.0

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 10850 Views - Last Post: 12 July 2011 - 08:45 PM

#1 DivideByZero   User is offline

  • D.I.C Addict
  • member icon

Reputation: 240
  • View blog
  • Posts: 551
  • Joined: 02-December 10

Figuring out how to handle content in xna 4.0

Posted 05 July 2011 - 12:03 PM

Evening all,

I haven't delved into XNA for a while now and I wanted to start a new project in which I aim to port it to Windows 7 phone as well as PC/Xbox 360.

To do this, I have to go out of my comfort zone and use a different version of XNA for the first time ever.

So my first hurdle that I can't find any good information on is how to import and use content in XNA 4.0.

I noticed that there is a separate project called "nameofgame\content" as well as a content reference that is in the actual game project. But no content folder that I'm use to adding files into.

So my question is, how do I import textures and assign them into a texture 2d in XNA 4.0?

Before I would just add them into the content folder and use this line of code:

this.tiles.Add(new Tile(Content.Load<Texture2D>("PM_MouseOffTile");



Any help is greatly appreciated :)

Is This A Good Question/Topic? 0
  • +

Replies To: Figuring out how to handle content in xna 4.0

#2 Kilorn   User is offline

  • XNArchitect
  • member icon



Reputation: 1361
  • View blog
  • Posts: 3,534
  • Joined: 03-May 10

Re: Figuring out how to handle content in xna 4.0

Posted 05 July 2011 - 12:07 PM

The method for loading and using textures hasn't really changed. The content project is just a place for you to put your content. It gives you a separate project for this in order to help keep things more easily organized. I generally place folders inside that project for specific types of content. For Textures, I have a folder named Textures, for Audio, a folder named Audio, etc. Then to load one of these, it's the same as it always has been:
Texture2D blah;

//Inside LoadContent method...
blah = Content.Load<Texture2D>("Textures\\blah");


Was This Post Helpful? 1
  • +
  • -

#3 DivideByZero   User is offline

  • D.I.C Addict
  • member icon

Reputation: 240
  • View blog
  • Posts: 551
  • Joined: 02-December 10

Re: Figuring out how to handle content in xna 4.0

Posted 05 July 2011 - 12:28 PM

For some reason it just won't work.

I right clicked the content project, added a new folder called Textures then added my png files into it.
I then used this line of code to assign it to a texture2d,

this.tiles = new List<Tile>();
            
this.tiles.Add(new Tile(Content.Load<Texture2D>("Textures\\PM_MouseOffTile"), 
Content.Load<Texture2D>("Textures\\PM_CrossTile"), 
                
new Vector2(new Random().Next(10, 790), new Random().Next(10, 590))));



but I keep getting told that the file could not be found :(

*Edit* The code formatting on this site is terrible :o

This post has been edited by DivideByZero: 05 July 2011 - 12:33 PM

Was This Post Helpful? 0
  • +
  • -

#4 Kilorn   User is offline

  • XNArchitect
  • member icon



Reputation: 1361
  • View blog
  • Posts: 3,534
  • Joined: 03-May 10

Re: Figuring out how to handle content in xna 4.0

Posted 05 July 2011 - 12:32 PM

And you're sure there is a reference to the content project in the game project as you mentioned before... Upload a quick screenshot of the solution explorer. Seems a bit strange that it's giving you that error if the reference is in place properly, and you've added the files to the content project inside the Textures folder.
Was This Post Helpful? 1
  • +
  • -

#5 DivideByZero   User is offline

  • D.I.C Addict
  • member icon

Reputation: 240
  • View blog
  • Posts: 551
  • Joined: 02-December 10

Re: Figuring out how to handle content in xna 4.0

Posted 05 July 2011 - 12:39 PM

I'm sure it's something really stupid on my half, here's the screenshot of my project :)

Attached Image

This post has been edited by DivideByZero: 05 July 2011 - 12:42 PM

Was This Post Helpful? 0
  • +
  • -

#6 Kilorn   User is offline

  • XNArchitect
  • member icon



Reputation: 1361
  • View blog
  • Posts: 3,534
  • Joined: 03-May 10

Re: Figuring out how to handle content in xna 4.0

Posted 05 July 2011 - 12:46 PM

I don't see anything that jumps out at me as obviously wrong. You have the files there, you didn't typo the name on any of the files, I can't see the content reference from the game project, but you said it was there. I'm kind of at a loss here.
Was This Post Helpful? 1
  • +
  • -

#7 DivideByZero   User is offline

  • D.I.C Addict
  • member icon

Reputation: 240
  • View blog
  • Posts: 551
  • Joined: 02-December 10

Re: Figuring out how to handle content in xna 4.0

Posted 05 July 2011 - 12:58 PM

I'm stumped too :(

I tried this instead,

Texture2D mouseOffTileTexture = Content.Load<Texture2D>("Textures\\PM_MouseOffTile");



but the exception still occurs saying "An unhandled exception of type 'Microsoft.Xna.Framework.Content.ContentLoadException' occurred in Microsoft.Xna.Framework.dll

Additional information: Error loading "Textures\PM_MouseOffTile". File not found.".
Was This Post Helpful? 0
  • +
  • -

#8 Kilorn   User is offline

  • XNArchitect
  • member icon



Reputation: 1361
  • View blog
  • Posts: 3,534
  • Joined: 03-May 10

Re: Figuring out how to handle content in xna 4.0

Posted 05 July 2011 - 01:04 PM

Try doing ("Content\\Textures\\PM_MouseOffTile");

I don't think this is going to make a difference, but I have seen it done this way before.
Was This Post Helpful? 1
  • +
  • -

#9 DivideByZero   User is offline

  • D.I.C Addict
  • member icon

Reputation: 240
  • View blog
  • Posts: 551
  • Joined: 02-December 10

Re: Figuring out how to handle content in xna 4.0

Posted 05 July 2011 - 01:10 PM

Didn't work, it threw the same exception.
I'm using Visual Studio Professional 2010 if that makes any difference.

*Edit* I opened up a new Visual Studio project and copied and pasted the code, but as you've probably guessed, the exact same exception occurred :(

This post has been edited by DivideByZero: 05 July 2011 - 01:22 PM

Was This Post Helpful? 0
  • +
  • -

#10 Kilorn   User is offline

  • XNArchitect
  • member icon



Reputation: 1361
  • View blog
  • Posts: 3,534
  • Joined: 03-May 10

Re: Figuring out how to handle content in xna 4.0

Posted 05 July 2011 - 01:26 PM

Expand "Content References" in the game project and upload a small screenshot of that for me. I have a strange feeling that there isn't something completely right with that. Also, did you rename any portions of the project and/or solution since you added that file?
Was This Post Helpful? 0
  • +
  • -

#11 ShadowsEdge19   User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: Figuring out how to handle content in xna 4.0

Posted 05 July 2011 - 01:26 PM

View PostDivideByZero, on 05 July 2011 - 02:10 PM, said:

Didn't work, it threw the same exception.
I'm using Visual Studio Professional 2010 if that makes any difference.


It doesn't since you have to have VS 2010 to run XNA 4.0. Try:

Texture2D mouseOffTileTexture = Content.Load<Texture2D>(@"Textures\PM_MouseOffTile");
Was This Post Helpful? 1
  • +
  • -

#12 ShadowsEdge19   User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: Figuring out how to handle content in xna 4.0

Posted 05 July 2011 - 01:36 PM

If the Content Reference is missing like Kilorn has a feeling about then this has happened to me before, but I know exactly what to do. Add Existing Project in the Solution Window and browse into the Content Folder and select the Content's .csproj file.

If you only open your main project using the .csproj file then you will most likely have only the main Project available and not the Content's Project too. To avoid this you must always open the .sln file that is a link to all projects involved. It took me awhile to figure this out but with the way Visual Studio 2010 handles XNA 4.0 it's the only way to properly handle your solution.
Was This Post Helpful? 1
  • +
  • -

#13 DivideByZero   User is offline

  • D.I.C Addict
  • member icon

Reputation: 240
  • View blog
  • Posts: 551
  • Joined: 02-December 10

Re: Figuring out how to handle content in xna 4.0

Posted 05 July 2011 - 01:40 PM

Didn't realise some of the solution explorer was hidden, here's the pic again.

Attached Image
Was This Post Helpful? 0
  • +
  • -

#14 DivideByZero   User is offline

  • D.I.C Addict
  • member icon

Reputation: 240
  • View blog
  • Posts: 551
  • Joined: 02-December 10

Re: Figuring out how to handle content in xna 4.0

Posted 07 July 2011 - 01:55 PM

I can't edit my most recent post so I'll make another post.
I just downloaded Visual C# Express 2010 and copied and pasted the code, and yet again it wouldn't work :(

No more XNA for me then :cry2:
Was This Post Helpful? 0
  • +
  • -

#15 ShadowsEdge19   User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: Figuring out how to handle content in xna 4.0

Posted 07 July 2011 - 01:57 PM

Upload your project, or one that is affected the same way, on something like dropbox and i'll try and see what's wrong.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2