2
votes

I'm working on a 2D fighting game with XNA (C#). Everything was going well in my game until I added a story mode. There are more than 60 frames. I have a variable whose value changes each time the person presses the spacebar. I wanted to experiment game state transitions and dispose() (or .Unload()) content when a screen was no longer needed. I have searched about Dispose() and Unload () to save memory that take my textures, but I can not give a value to my variable Texture2D after having called Dispose() for it. (texture.IsDisposed = true).

I have created a ContentManager xContent in a Manager Class.

 Texture2D texture;
 int image = 0;
 ____________________

 if (Keyboard.GetState().IsKeyDown(Keys.Space))
 {
     if (image == 0)
     {
         texture = Manager.xContent.Load<Texture2D>("texture1");
     }
     else if (image == 1)
     {
         texture.Dispose();
         texture = Manager.xContent.Load<Texture2D>("texture2");
     }
 }

Draw :

spriteBatch.Begin();
...
spriteBatch.End();

I received an error on spriteBatch.End() of the above paste because I have reloaded the texture. What should I do?

2

2 Answers

2
votes

You should not be calling 'Dispose()' on any of the content which was loaded by the XNA content manager. The content manager will manage the lifespan of the content (that's it's job, after all). It ought to make reasonable decisions about when to load/unload specific textures based on usage.

When the content manager itself is disposed then it will dispose all of the content that it's currently managing, so you don't need to dispose individual pieces of content.

0
votes

Try to to organize it inside your manager. Loading the appropriate texture to the collection as needed and use them and remove if they become do not really need. If the texture will not link it sooner or later pick up the garbage collector.