Windows phone 7 WP7墓碑,将精灵保存到当前状态不工作
我正在制作WP7游戏。我正在使用游戏状态管理(http://create.msdn.com/en-US/education/catalog/sample/game_state_management ,但我认为这并不重要)我在将数据保存到 Microsoft.Phone.Shell.PhoneApplicationService.Current.State 如果我用这种方法放雪碧的话Windows phone 7 WP7墓碑,将精灵保存到当前状态不工作,windows-phone-7,tombstoning,Windows Phone 7,Tombstoning,我正在制作WP7游戏。我正在使用游戏状态管理(http://create.msdn.com/en-US/education/catalog/sample/game_state_management ,但我认为这并不重要)我在将数据保存到 Microsoft.Phone.Shell.PhoneApplicationService.Current.State 如果我用这种方法放雪碧的话 public override void Deactivate() { #if WI
public override void Deactivate()
{
#if WINDOWS_PHONE
Microsoft.Phone.Shell.PhoneApplicationService.Current.State["Score"] = Score;
Microsoft.Phone.Shell.PhoneApplicationService.Current.State["cloudSprite"] = cloudSprite;
#endif
base.Deactivate();
}
这里面什么都没有
Microsoft.Phone.Shell.PhoneApplicationService.Current.State
在激活方法中。然而,如果我删除cloudSprite并只放在int的分数上,它就可以正常工作了。我不知道怎么了,也许它不能处理更复杂的物体。我也试过浮球,一切正常。但是如果我把更复杂的东西放在那里,它就不起作用了。你觉得怎么样
编辑
这是我的雪碧课。我不知道如何使它可序列化。我在那里添加了[DataContractAttribute()]和[DataMember],但它不起作用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.Runtime.Serialization;
using System.IO;
namespace GameStateManagementSample.GameObjects
{
[DataContractAttribute()]
public class Sprite
{
[DataMember]
public Vector2 Position;
[DataMember]
public Vector2 Size;
[DataMember]
public Texture2D Texture;
[DataMember]
public Rectangle Rect
{
get
{
return new Rectangle((int)Position.X, (int)Position.Y, (int)Size.X, (int)Size.Y);
}
}
public Sprite(Vector2 position)
{
Position = position;
}
public Sprite(Vector2 position, Vector2 size)
{
Position = position;
Size = size;
}
public Sprite(Vector2 position, Texture2D texture)
{
Position = position;
Texture = texture;
Size = new Vector2(Texture.Width, Texture.Height);
}
public void LoadContent(string assetName, ContentManager content)
{
Texture = content.Load<Texture2D>(assetName);
if (Size == Vector2.Zero)
Size = new Vector2(Texture.Width, Texture.Height);
}
public virtual void Draw(SpriteBatch spriteBatch)
{
//spriteBatch.Draw(Texture, Rect, Color.White);
spriteBatch.Draw(Texture, Position, Color.White);
}
public virtual void Draw(SpriteBatch spriteBatch, Rectangle TexturePositionInSpriteSheet, Color color)
{
spriteBatch.Draw(Texture, Position, TexturePositionInSpriteSheet, color);
}
public bool Intersects(Vector2 point)
{
if (point.X >= Position.X && point.Y >= Position.Y && point.X <= Position.X + Size.X && point.Y <= Position.Y + Size.Y)
return true;
else return false;
}
public bool Intersects(Rectangle rect)
{
return Rect.Intersects(rect);
}
public static void Serialize(Stream streamObject, object objForSerialization)
{
if (objForSerialization == null || streamObject == null)
return;
DataContractSerializer ser = new DataContractSerializer(objForSerialization.GetType());
ser.WriteObject(streamObject, objForSerialization);
}
public static object Deserialize(Stream streamObject, Type serializedObjectType)
{
if (serializedObjectType == null || streamObject == null)
return null;
DataContractSerializer ser = new DataContractSerializer(serializedObjectType);
return ser.ReadObject(streamObject);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Graphics;
使用Microsoft.Xna.Framework.Content;
使用System.Runtime.Serialization;
使用System.IO;
命名空间GameStateManagementSample.GameObjects
{
[DataContractAttribute()]
公共级雪碧
{
[数据成员]
公共向量2位置;
[数据成员]
公共向量2大小;
[数据成员]
公共纹理2D纹理;
[数据成员]
公共矩形矩形
{
得到
{
返回新矩形((int)Position.X,(int)Position.Y,(int)Size.X,(int)Size.Y);
}
}
公共精灵(矢量2位置)
{
位置=位置;
}
公共精灵(矢量2位置,矢量2大小)
{
位置=位置;
大小=大小;
}
公共精灵(矢量2位置,纹理2D纹理)
{
位置=位置;
纹理=纹理;
大小=新矢量2(纹理.宽度,纹理.高度);
}
公共void加载内容(字符串assetName、ContentManager内容)
{
纹理=content.Load(assetName);
if(大小==矢量2.0)
大小=新矢量2(纹理.宽度,纹理.高度);
}
公共虚拟空白绘制(SpriteBatch SpriteBatch)
{
//绘制(纹理、矩形、颜色、白色);
绘制(纹理、位置、颜色、白色);
}
公共虚拟空白绘制(SpriteBatch SpriteBatch、矩形纹理位置列表、颜色)
{
绘制(纹理、位置、纹理位置、颜色);
}
公共布尔交叉点(矢量2点)
{
如果(point.X>=Position.X&&point.Y>=Position.Y&&point.X我相信您会得到ArgumentOutOfRangeException,如果您不进行调试(在这种情况下会引发异常),则会自动忽略该ArgumentOutOfRangeException。存储在状态字典中的项需要可序列化。请尝试以某种方式存储该精灵,并仅保存链接(源代码)添加到状态字典中的精灵。添加到状态
集合的对象使用DataContractSerializer进行序列化。确保在那里保存的任何内容都可以通过这种方式进行序列化。
任何序列化错误都将被静默忽略
更新
以下是精灵对象的简化版本:
[DataContract]
public class Sprite
{
[DataMember]
public Vector2 Position;
[DataMember]
public Vector2 Size;
[DataMember]
public Texture2D Texture;
public Sprite()
{
}
public Sprite(Vector2 position)
{
Position = position;
}
public Sprite(Vector2 position, Vector2 size)
{
Position = position;
Size = size;
}
public Sprite(Vector2 position, Texture2D texture)
{
Position = position;
Texture = texture;
Size = new Vector2(Texture.Width, Texture.Height);
}
}
下面是一个被序列化和反序列化的示例:
//Sprite系列化测试
var sprite1=新精灵(新矢量2(12.34f,56.78f))
Sprite-sprite2;
使用(var memStr=new MemoryStream())
{
var serializer=新的DataContractSerializer(typeof(Sprite));
serializer.WriteObject(memStr,sprite1);
memStr.Position=0;
var sr=新的StreamReader(memStr);
var serialized=sr.ReadToEnd();
//序列化现在看起来像
// 12.3456.7800
memStr.Position=0;
sprite2=(Sprite)序列化程序.ReadObject(memStr);
//sprite2现在包含与
//sprite2.Position={X:12.34,Y:56.78}
}
据我所知,我没有收到任何异常。我不确定我的精灵是否被标记为可序列化。我回家后会通知你。哦,谢谢你,我编辑了我的帖子。我无法使其可序列化。我的代码在那里。请看它。首先映入我眼帘的是,你没有无参数构造函数。这是你需要的用于反序列化。或者,您可以自己序列化(并反序列化)对象,然后只将结果字符串添加到状态对象。@Mattlesy AFAIKDataContractSerializer
不需要无参数构造函数,XmlSerialiser
需要一个
Sprite sprite2;
using (var memStr = new MemoryStream())
{
var serializer = new DataContractSerializer(typeof(Sprite));
serializer.WriteObject(memStr, sprite1);
memStr.Position = 0;
var sr = new StreamReader(memStr);
var serialized = sr.ReadToEnd();
// serialized now looks like
// <Sprite xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/MiscExperiments"><Position xmlns:d2p1="http://schemas.datacontract.org/2004/07/Microsoft.Xna.Framework"><d2p1:X>12.34</d2p1:X><d2p1:Y>56.78</d2p1:Y></Position><Size xmlns:d2p1="http://schemas.datacontract.org/2004/07/Microsoft.Xna.Framework"><d2p1:X>0</d2p1:X><d2p1:Y>0</d2p1:Y></Size><Texture xmlns:d2p1="http://schemas.datacontract.org/2004/07/Microsoft.Xna.Framework.Graphics" i:nil="true" /></Sprite>
memStr.Position = 0;
sprite2 = (Sprite)serializer.ReadObject(memStr);
// sprite2 now contains the same as
// sprite2.Position = { X:12.34, Y:56.78 }
}