Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'd like to build an engine in XNA, primarily for a 2D RPG. At first, I began abstracting out some classes and built a Sprite class which wrapped a Vector2 and a Texture2D, and tried loading in content from the Sprite class' constructor. However, this didn't work. Now, it appears that I have to load all of the sprites within the Game class' LoadContent method. Is there anyway around this? I'd like to separate the actual Game Content from the inner workings of the engine, if possible.

share|improve this question
add comment

3 Answers

up vote 6 down vote accepted

THe LoadContent is simply called after the graphics device has been initialized. You can load content at any point after that, not just in the LoadContent method.

If you were, for instance, to instantiate your sprite class within the LoadContent method, or within the Draw or Update calls, you wouldn't have any problem.

Now, as far as design goes, having the sprite load the content it needs typically isn't a good idea. To keep with the single responsibility principle, and dependency inversion principle, you might want to accept a Texture2D as a parameter to the Sprite constructor. That way you'll be able to have the same "BadGuy" sprite, but be able to pass it two different textures (for say, a red bad guy and a green bad guy).

share|improve this answer
add comment

You don't need to load all content within the LoadContent method, you could load it in a method called by that, or at pretty much any time after that (once the GraphicsDevice is set up).

A very simple example might be having a list or similar structure in your game engine, then before loading the user of the engine would add the locations and types of content to load. Then you'd call your engine's LoadContent method from your game's LoadContent method. The engine would then loop through the added resources and load them for use in the engine.

This is of course a simple example, you'd probably need some kind of resource manager to manage loading/unloading/making resources available to what needs them etc.

share|improve this answer
add comment

what if you have this constructor? (i mean, is this possible?)

Sprite(Microsoft.Xna.Framework.Content.ContentManager content){
    Texture2D something = content.Load<Texture2D>("someImages");
}
share|improve this answer
2  
Answer if you know, comment if you guess. –  Anko Mar 12 '13 at 11:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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