MS VS 2010,XNA 4.0
所以,我有一个行星&它具有保存和加载功能。简单的二进制读写器代码不工作
public void SaveToFile(ContentManager content)
{
string path = content.RootDirectory + @"\Objects\Planets\" + this.name [email protected]".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”
,这是在该行
这种情况发生。 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函数,而没有其他。另外,我在我的项目中保存了我的内容中的文件。我曾经说过要将该文件视为内容(不要编译它)。所以,代码“看到”这个文件,这对于shure来说,但是它不能从mars.planet读取的路径中找到.png。如果我一个接一个地存储2个字符串,那么读者可能不知道第一个字符的结尾在哪里?我可能保存错了吗?
我的目标是让二进制文件被加载和保存,并且它们是行星,火箭,地图等,它们是这些类的构造函数所需的名称和值的集合。
你写'planet.FPS'为'double',但是你试着把它看作一个'decimal'? – cubrr
错误消息告诉你有一个名为'assetName'的参数,当需要非空值时,它的值为'null'。当然,这与'BinaryReader'没有直接关系。至于它可能是什么,除非您提供可靠地再现问题的[良好_minimal_,_complete_代码示例](http://stackoverflow.com/help/mcve),否则不可能说。 –
FPS是一个浮动。二进制写入器读取器没有浮点数,所以当我保存一个浮点数时,我将其保存为十进制数,当我加载它时,我加载小数点并将其转换为浮点数:(float)br.ReadDecimal();. – Monset