【发布时间】:2015-06-20 23:51:58
【问题描述】:
MS VS 2010,XNA 4.0
所以,我有一个类 Planet,它具有保存和加载功能。
public void SaveToFile(ContentManager content)
{
string path = content.RootDirectory + @"\Objects\Planets\" + this.name +@".planet";
using (BinaryWriter bw = new BinaryWriter(File.Open(path, FileMode.Create)))
{
bw.Write(name);
//sprite names
bw.Write(planet.Sprite.Name); //"planet" is an animation.
bw.Write(planet.HorFrames); //and thats why it has a sprite
bw.Write(planet.VerFrames); //that has a name
bw.Write((double)planet.FPS);
bw.Write(asteroid1.Name);
bw.Write(asteroid2.Name);
bw.Write(asteroid3.Name);
bw.Write(backgroundA.Name);
//update related
bw.Write((double)RSpeed);
bw.Write((double)ASVariety);
bw.Write((double)A1Chance);
bw.Write((double)A2Chance);
bw.Write((double)A3Chance);
bw.Close();
}
}
public void LoadFromFile(ContentManager content, string name)
{
string path = content.RootDirectory + @"\Objects\Planets\" + name + @".planet";
using (BinaryReader br = new BinaryReader(File.OpenRead(path)))
{
this.name = br.ReadString();
this.planet = new Animation(Cont.Texture2D(br.ReadString()), br.ReadInt32(), br.ReadInt32(), (float)br.ReadDecimal(), true);
this.asteroid1 = Cont.Texture2D(br.ReadString());
this.asteroid2 = Cont.Texture2D(br.ReadString());
this.asteroid3 = Cont.Texture2D(br.ReadString());
this.backgroundA = Cont.Texture2D(br.ReadString());
this.RSpeed = (float)br.ReadDouble();
this.ASVariety = (float)br.ReadDouble();
this.A1Chance = (float)br.ReadDouble();
this.A2Chance = (float)br.ReadDouble();
this.A3Chance = (float)br.ReadDouble();
bposB = new Vector2(backgroundA.Width - 1, 0);
bspd = new Vector2(-RSpeed / 8f, 0);
pEngine1 = new ParticleEngine(MSTime, 40, new Vector2(20, -30), new Vector2(2, 1), asteroid1, new Color(100, 100, 100));
pEngine1.Follow(new Vector2(-200, Game1.window_height / 2));
pEngine2 = new ParticleEngine(MSTime * 3, 40, new Vector2(20, -30), new Vector2(2, 1), asteroid2, new Color(100, 100, 100));
pEngine2.Follow(new Vector2(-200, Game1.window_height / 2));
br.Close();
}
这很容易理解,不是吗?
现在,当我尝试为行星动画加载精灵时,我收到一个错误,上面写着:
"值不能为空。
参数名称:assetName"
这发生在生产线上
this.planet = new Animation(Cont.Texture2D(br.ReadString()), br.ReadInt32(), br.ReadInt32(), (float)br.ReadDecimal(), true);
Cont.Texture2D 是一个静态函数,它返回一个 Texture2D 女巫名称等于它加载的路径,它看起来像这样:
public static Texture2D Texture2D(string path)
{
Texture2D sprite = content.Load<Texture2D>(path);
sprite.Name = path;
return sprite;
}
所以,当我保存它时,它会保存正确的路径。测试函数如下所示:
private void testFunction()
{
/*
p = new Planet("Mars",
new Animation(Cont.Texture2D("Sprites\\Planets\\mars"), 8, 2, 0.9f, true),
Cont.Texture2D("Sprites\\Backgrounds\\space"),
Cont.Texture2D("Sprites\\Asteroids\\small\\small 1"),
Cont.Texture2D("Sprites\\Asteroids\\medium\\medium 1"),
Cont.Texture2D("Sprites\\Asteroids\\big\\big 1"),
100, 50, 40, 20, 40, 40);
p.SaveToFile(Content);
*/
p = new Planet(Content, "Mars");
tests = p.ToString();
}
"tests" 只是一个测试字符串。现在,我首先执行注释代码,以便保存文件,然后执行未注释代码。这个行星构造函数只调用 LoadFromFile 函数,仅此而已。另外,我的项目内容中有保存的文件。我确实说过将该文件视为内容(不要编译它)。因此,代码“看到”了文件,这就是舒尔,但它无法在从 mars.planet 读取的路径上找到 .png。如果我一个接一个地存储 2 个字符串,然后读者看不到第一个字符串的结尾在哪里,可能会有错误吗?我可能保存错了吗?
我的目标是拥有将被加载和保存的二进制文件,其中包括行星、火箭、地图等,它们是这些类的构造函数所需的名称和值的集合。
【问题讨论】:
-
您将
planet.FPS写为double,但您尝试将其读为decimal? -
错误信息告诉你有一个名为
assetName的参数,当需要非空值时,其值为null。当然,这与BinaryReader没有直接关系。至于它可能是什么,除非您提供可靠地重现问题的a good, minimal, complete code example,否则这是不可能的。 -
FPS 是一个浮点数。二进制读写器没有浮点数,所以当我保存浮点数时,我将它保存为十进制,当我加载它时,我加载小数并将其转换为浮点数:(float)br.ReadDecimal();。
-
@PeterDuniho :我没有更多的代码可以帮助您理解我的问题。我让“Cont”成为一个可以加载纹理/精灵/音乐的静态类......我在运行测试功能之前加载了它,这意味着它可以正常运行。我还在其他精灵路径上对其进行了测试,并且可以正常工作。在准备好 Cont 类之后,我调用了测试函数。我不知道如何才能使这个更优秀、更简洁、更完整的代码示例可靠地重现问题。
标签: c# file xna binaryreader binarywriter