following problem.
I have a XML-file:
<Question>
<questionTxt>In welchem US-Bundesstaat befindet sich der "Yellowstone National Park?"</questionTxt>
<a>"Wyoming"</a>
<b>"Illinois"</b>
<c>"Iowa"</c>
<d>"Mississippi"</d>
<texturePath>"YellowStone"</texturePath&g开发者_JAVA技巧t;
</Question>
In another Class I read all the values for a Node like this. The problem is all my LoadContent-methods look like this:
internal static void LoadContent(ContentManager Content)
{
currentTex = Content.Load<Texture2D>("gfxData/" + texturePath);
}
So they are internal and static and can be called only one time for a class. But what if I read another node so the "texturePath" changes. How to reload the image texture?
Due to the fact you have this problem your framework is probably has some major flaws. Anyways the only thing I can suggest from such a limited description is to use a Dictionary as a static variable using the keys as the content names. If the key doesn't exists load the content. If it does just return the value.
What you need to do (For XNA 4.0) is:
Add a Windows Game Project to your existing project. This will hold a class that reads the XML as a List
Ensure you have the following references
using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content;
Make a namespace that describes the project (EnemyData), and inside that, a public class that holds the data (ENEMIES).
Add the items you want to read from the XML as PUBLIC variables. It's important they be public or it won't work. You'll get an error like "Element at line 4, character 7 is not valid."
Right click on the Content Folder, and add an XML file, or copy over your XML file.
Ensure the XML file uses
<XnaContent>
as the ROOT node.Ensure the XML file uses
<Asset Type="namespace.classname[]">
, for example<Asset Type="EnemyData.ENEMIES[]">
Use a node
<Item>
for each item you're creating, even if there's only one. In my case, I need an<Item>
for each enemy.Set the XML file's Content Importer to XML Content - XNA Framework, and the Content Processor to No Processing Required.
In your main program (or game), make sure you right click on the main project and add a reference to the class you made.
You also have to do this for the Content Project. Very Important. Both the main project and the Content Project must have the reference to your class.
In the main/game Game1, add a Using statement for your class.
// My Enemy Data XML Class NAMESPACE
using EnemyData;
Create a variable for the class, such as:
//Enemy Data
ENEMIES[] myEnemyData;
Load the content in the LoadContent procedure:
// Load the Enemy data table
myEnemyData = Content.Load("Enemies");
Use the data from the XML file
enemyTexture = Content.Load(myEnemyData[3].enemyTexture);
Damage = MyEnemyData[3].Damage;
That's it! Just need to make sure that the type in the XML's Nodes matches the type in the class, and that they're public classes. I don't want to post all the code and the XML here, but here are links to them.
http://www.DCJoys.com/Class1.cs
http://www.DCJoys.com/EnemyData.csproj
http://www.DCJoys.com/Enemies.xml
I believe the answer is Wyoming.
Seriously though, I think you are confusing static methods and static variables. The static method can be called many times. Static methods are class level and don't require you to create an instance of the class.
精彩评论