我从C#和XNA开始。我正在尝试使用一个小库来使用基于组件的设计。
当我尝试使用XML在游戏中制作实体时出现问题。编译器发送该错误:
Error 1 Building content threw NullReferenceException: Object reference not set to an instance of an object.
at ReflectionEmitUtils(Object , Object )
at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.ReflectiveSerializerMemberHelper.DeserializeRegularObject(IntermediateReader input, Object parentInstance)
at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.ReflectiveSerializerMemberHelper.Deserialize(IntermediateReader input, Object parentInstance)
at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.ReflectiveSerializer.Deserialize(IntermediateReader input, ContentSerializerAttribute format, Object existingInstance)
at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.IntermediateReader.ReadRawObjectInternal[T](ContentSerializerAttribute format, ContentTypeSerializer typeSerializer, Object existingInstance)
at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.IntermediateReader.ReadObjectInternal[T](ContentSerializerAttribute format, ContentTypeSerializer typeSerializer, Object existingInstance)
at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.IntermediateReader.ReadObject[T](ContentSerializerAttribute format, ContentTypeSerializer typeSerializer)
at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.DictionarySerializer`2.Deserialize(IntermediateReader input, ContentSerializerAttribute format, Dictionary`2 existingInstance)
at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.ContentTypeSerializer`1.Deserialize(IntermediateReader input, ContentSerializerAttribute format, Object existingInstance)
at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.IntermediateReader.ReadRawObjectInternal[T](ContentSerializerAttribute format, ContentTypeSerializer typeSerializer, Object existingInstance)
at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.IntermediateReader.ReadObjectInternal[T](ContentSerializerAttribute format, ContentTypeSerializer typeSerializer, Object existingInstance)
at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.ReflectiveSerializerMemberHelper.DeserializeRegularObject(IntermediateReader input, Object parentInstance)
at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.ReflectiveSerializerMemberHelper.Deserialize(IntermediateReader input, Object parentInstance)
at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.ReflectiveSerializer.Deserialize(IntermediateReader input, ContentSerializerAttribute format, Object existingInstance)
at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.IntermediateReader.ReadRawObjectInternal[T](ContentSerializerAttribute format, ContentTypeSerializer typeSerializer, Object existingInstance)
at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.IntermediateReader.ReadObjectInternal[T](ContentSerializerAttribute format, ContentTypeSerializer typeSerializer, Object existingInstance)
at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.IntermediateReader.ReadObject[T](ContentSerializerAttribute format)
at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.IntermediateSerializer.Deserialize[T](XmlReader input, String referenceRelocationPath)
at Microsoft.Xna.Framework.Content.Pipeline.XmlImporter.Import(String filename, ContentImporterContext context)
at Microsoft.Xna.Framework.Content.Pipeline.ContentImporter`1.Microsoft.Xna.Framework.Content.Pipeline.IContentImporter.Import(String filename, ContentImporterContext context)
at Microsoft.Xna.Framework.Content.Pipeline.BuildCoordinator.ImportAssetDirectly(BuildItem item, String importerName)
at Microsoft.Xna.Framework.Content.Pipeline.BuildCoordinator.ImportAsset(BuildItem item)
at Microsoft.Xna.Framework.Content.Pipeline.BuildCoordinator.BuildAssetWorker(BuildItem item)
at Microsoft.Xna.Framework.Content.Pipeline.BuildCoordinator.BuildAsset(BuildItem item)
at Microsoft.Xna.Framework.Content.Pipeline.BuildCoordinator.RunTheBuild()
at Microsoft.Xna.Framework.Content.Pipeline.Tasks.BuildContent.RemoteProxy.RunTheBuild(BuildCoordinatorSettings settings, TimestampCache timestampCache, ITaskItem[] sourceAssets, String[]& outputContent, String[]& rebuiltContent, String[]& intermediates, Dictionary`2& dependencyTimestamps, KeyValuePair`2[]& warnings) c:\Users\Agustin\Documents\Visual Studio 2010\Projects\LibraryTest\LibraryTest\LibraryTestContent\XML\Circle.xml LibraryTest
我知道xml文件存在问题,因为如果我将“Build action”属性设置为None,则此错误消失。 xml:
<?xml version="1.0" encoding="utf-8" ?>
<XnaContent xmlns:Framework="Microsoft.Xna.Framework"
xmlns:Graphics="Microsoft.Xna.Graphics"
xmlns:CAEL="ComponentsAndEntitiesLibrary"
xmlns:Components="ComponentsAndEntitiesLibrary.BasicComponents">
<Asset Type="CAEL:Entity">
<HeavyComponents>
<Item>
<Key>Sprite</Key>
<Value Type="Components:SpriteSheet">
<SpriteFile>Images/threerings</SpriteFile>
<XSheet>0</XSheet>
<YSheet>0</YSheet>
<HeightSheet>8</HeightSheet>
<WidthSheet>6</WidthSheet>
</Value>
</Item>
</HeavyComponents>
<LightComponents>
<Item>
<Key>Color</Key>
<Value Type="Components:Color">
<ColorValue Null="true" />
</Value>
</Item>
<Item>
<Key>Position</Key>
<Value Type="Components:Position">
<X>0</X>
<Y>0</Y>
</Value>
</Item>
<Item>
<Key>Metrics</Key>
<Value Type="Components:Metrics">
<Height>75</Height>
<Width>75</Width>
</Value>
</Item>
</LightComponents>
<Id>Ring</Id>
</Asset>
</XnaContent>
这就是我使用的课程:
Entity.cs
namespace ComponentsAndEntitiesLibrary
{
public class Entity : HeavyComponent
{
private Dictionary<string, Component> heavyComponents;
private Dictionary<string, Component> lightComponents;
private string id;
public Entity(string id)
: base(id)
{
this.id = id;
heavyComponents = new Dictionary<string, Component>();
lightComponents = new Dictionary<string, Component>();
}
public Entity()
: base("")
{
this.id = "";
heavyComponents = new Dictionary<string, Component>();
lightComponents = new Dictionary<string, Component>();
}
public Dictionary<string, Component> HeavyComponents
{
get { return heavyComponents; }
set
{
heavyComponents = HeavyComponents;
foreach (Component comp in HeavyComponents.Values) addComponent(comp);
}
}
public Dictionary<string, Component> LightComponents
{
get { return lightComponents; }
set
{
lightComponents = LightComponents;
foreach (Component comp in LightComponents.Values) addComponent(comp);
}
}
public string Id
{
get { return id; }
set { id = Id; }
}
blablablablablablablabla
}
}
颜色
namespace ComponentsAndEntitiesLibrary
{
namespace BasicComponents
{
public class Color : LightweightComponent
{
private const string name = "Color";
private Microsoft.Xna.Framework.Color color;
public Color()
: base(name)
{
color = Microsoft.Xna.Framework.Color.White;
}
public Color(Microsoft.Xna.Framework.Color color)
: base(name)
{
this.color = color;
}
public Microsoft.Xna.Framework.Color ColorValue
{
get { return color; }
set { color = ColorValue; }
}
}
}
}
Position.cs:
namespace ComponentsAndEntitiesLibrary
{
namespace BasicComponents
{
public class Position : LightweightComponent
{
private const string name = "Position";
private float x;
private float y;
private float z;
public Position()
: base(name)
{
x = y = z = 0;
}
public Position(float x, float y)
: base(name)
{
this.x = x;
this.y = y;
z = 0;
}
public Position(float x, float y, float z)
: base(name)
{
this.x = x;
this.y = y;
this.z = z;
}
public float X
{
get { return x; }
set { this.x = X; }
}
public float Y
{
get { return y; }
set { this.y = Y; }
}
public float Z
{
get { return z; }
set { this.z = Z; }
}
blablablablablablablablabla
}
}
}
Metrics.cs
namespace ComponentsAndEntitiesLibrary
{
namespace BasicComponents
{
public class Metrics : LightweightComponent
{
private const string name = "Metrics";
private float height;
private float width;
public Metrics()
: base(name)
{
height = width = 0;
}
public Metrics(float height, float width)
: base(name)
{
this.height = height;
this.width = width;
}
public float Height
{
get { return height; }
set { height = Height; }
}
public float Width
{
get { return width; }
set { width = Width; }
}
}
}
}
SpriteSheet.cs:
namespace ComponentsAndEntitiesLibrary
{
namespace BasicComponents
{
public class SpriteSheet : Sprite
{
private int xSheet;
private int ySheet;
private int heightSheet;
private int widthSheet;
private Metrics metrics;
public SpriteSheet()
: base()
{
xSheet = 0;
ySheet = 0;
}
public SpriteSheet(string spriteFile)
: base(spriteFile)
{
xSheet = 0;
ySheet = 0;
}
public int XSheet
{
get { return xSheet; }
set { xSheet = XSheet; }
}
public int YSheet
{
get { return ySheet; }
set { ySheet = YSheet; }
}
public int HeightSheet
{
get { return heightSheet; }
set { heightSheet = HeightSheet; }
}
public int WidthSheet
{
get { return widthSheet; }
set { widthSheet = WidthSheet; }
}
blablablablablablabla
}
}
}
可能是一个愚蠢的问题,但我没有经验,我看不到它。
感谢。
编辑:
解决。更改Color类以使属性Color可选,并从xml中删除该错误消失。然而,我不知道为什么。