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();
}
“测试”只是一个测试字符串。现在,我首先执行注释代码,以便保存文件,然后执行未注释代码。这个行星构造函数只调用 LoadFromFile 函数,仅此而已。另外,我的项目内容中有保存的文件。我确实说过将该文件视为内容(不要编译它)。因此,代码“看到”了文件,这就是舒尔,但它无法在从 mars.planet 读取的路径上找到 .png。如果我一个接一个地存储 2 个字符串,然后读者看不到第一个字符串的结尾在哪里,可能会有错误吗?我可能保存错了吗?
我的目标是拥有将被加载和保存的二进制文件,其中包括行星、火箭、地图等,