views:

547

answers:

2

I have created a separate class (let's call it class2.cs for example) and want to use it as a level, in that when I call it, it will draw everything in one level for me. I'm having trouble getting contentmanager to work in class2. In the given Game1.cs, you can easily just go texture2d= Content.Load<Texture2D>("photo"); but I can't in class2.

I realize I have to create a new Content Manager, but it's constructor requires a game service, in which I'm not sure what I'm suppose to plug in. I currently have: ContentManager content = new ContentManager(); but I need an overload for ContentManager.

+3  A: 

Pass Content to the constructor of your second class from the game, or you can create a Globals.cs class with static variables for your ContentManager or spriteBatch or any common resources.

Smelch
Do not use globals - use dependency injection instead (http://www.codeplex.com/unity)
BlueRaja - Danny Pflughoeft
EDIT: Never mind fixed, I had to load contentmanager after initialize, not as in the constructor.
DMan
@BlueRaja, "pass Content to the constructor of your second class", that is dependency injection, btw
Allen
A: 

how did you do that DMan? i'm interested in loading multiple content manager too in separate classes.

FireFox
In my second, custom class, I added a load method looking like `void LoadContent(ContentManager Content)` Then I simply used `Content.Load` like normal. In my class one, I would have included `Class2.LoadContent`. `LoadContent` would have to be either public, or static. Hope that helped.
DMan