Load object into another class's List XNA C#

Say you have a ScreenManager which inherits DrawableGameComponent, and a GamePlayScreen which inherits GameScreen. And GamePlayScreen is drawn through ScreenManagers ContentManager, and functions just like Game1.cs would. And its LoadContent looks like:

public override void LoadContent()

if (content == null)
content = new ContentManager(ScreenManager.Game.Services, "Content")

foreach (Objects object in objects)
   object.LoadContent();

And object class calls same LoadContent method, but of course says object = content.Load<Texture2D>(" - "); ... I'm getting the error:

"content = new ContentManager(ScreenManager.Game.Services, "Content") - Object reference not set to an instance of an object." In my objects class LoadContent method.

Is there a way to call GamePlayScreen's content or ContentManager, from the object class? +++++++++++++++++++++++

Ok, I've realized what exactly the problem is. I'm using the Logic of the GameStateManagement Sample, "created by the head tech of Xbox Indie Games", which is in its own right is brilliant code... If all you're doing is making a main menu... lol...

BUT, ScreenManager is the DrawableGameComponent, its list of GameScreen's makes it and said screen the only functioning things in the Game. Or possibly only the screens are, and when they transition off ScreenManager is called, wakes up, adds new screen and goes to sleep again.

Thus NOT allowing you to call any classes, object list's, or anything contained in another class...

Now one may think, just create a new ScreenManager2 : DrawableGameComponent , or maybe even an ObjectManager : DrawableGameComponent .... Ahh ahhh, already beat you to it friend.....

Creating a new ObjectManager allows your objects to be drawn over everything your original ScreenManager deals with... True;... including your PauseScreen, and PauseMenu.... Garbage!

And before you mention a ScreenManager2, that to handle PauseScreen and PauseMenu, tried that as well. I successfully manipulated all code to do so, up until Play was selected for ScreenManager to tell LoadingScreen to function, then call GamePlayScreen...

Which leads to Asik being right, my content is Null, because I cannot figure out a way for my object class/classes to be called and Initialized with the way this code works.

Which leads to me asking the question correctly...

How do you call a class to Initialize, just long enough to allow LoadContent in the GamePlayScreen to gather said classes/objects information, to be put in its object list?

Lehman's terms: "GamePlayScreen on your LoadContent function, wake up Object1.cs, add object information in your ObjectList, tell Object1.cs to sleep, GamePlayScreen go on with your bad self..." ???


You are making use of GameComponent and DrawableGameComponent . Your subclasses that inherit from these have access to the Game instance via their GameComponent.Game property (that is, if you've passed it into their constructors rather than specifying null .

This is good. Just use GameComponent.Game.Content to access the ContentManager in each.


"Object reference not set to an instance of an object" is a NullReferenceException : it means you're trying to access something from a variable that is currently null. On the line you have posted, I see two possibilities: either ScreenManager.Game is null, or ScreenManager.Game.Services is null. Why? Because you're not initializing them by the time this line gets called.

That said, why not just pass the ContentManager that whatever class needs it rather than instantiate new ones?


With the complexity of ScreenManager, I found no way to load the content directly from the Object1 class, I was however able to call an older form of object handling:

In Object1 class...

Texture2D image;

public Object1(Texture2D texture)
{
  this.image = texture;
  //other information...
}

Even though SpriteBatch was never declared in the objects class, I was able to call:

public override void Draw(SpriteBatch spriteBatch)
{
    spriteBatch.Draw(image, Rectangle, Color.White);
}

And in GamePlayScreen's LoadContent:

Texture2D tempImage = content.Load...
objectList.Add(new Object1(tempImage));

And then in GamePlayScreen's Draw function which calls for GameTime, I called:

foreach (Objects object in objectList)
    object.Draw(spriteBatch);

After 2 weeks later, this is finally resolved... And I quote myself:

"It all looks so easy, until you're the one who needs to figure it out..."

链接地址: http://www.djcxy.com/p/95522.html

上一篇: XNA加载/卸载逻辑(contentmanager?)

下一篇: 将对象加载到另一个类的列表中XNA C#