我想测量阻力的时间。从开始(当玩家触摸屏幕时)到结束(当玩家从屏幕释放他的手指时)。 另外,我想测量阻力的距离来计算阻力的速度。
但我总是收到以下两条错误消息。
1)当我触摸屏幕时,我在foreach循环的第一行收到此错误消息:
GestureSample gs = TouchPanel.ReadGesture();
Microsoft.Xna.Framework.Input.Touch.ni.dll中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理 如果存在此异常的处理程序,则可以安全地继续该程序。
2)第二条错误消息在此行中:
DragTime = (float)(EndTime - StartTime);
无法将'System.DateTime'类型转换为'float'
有什么问题?我该怎么做才能修复错误消息?
这是我的代码:
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Vector2 DragVelocity;
TimeSpan StartTime;
DateTime EndTime;
float DragTime;
Vector2 StartPoint, EndPoint, DragDistance;
bool FirstTimePressed = true;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
TargetElapsedTime = TimeSpan.FromTicks(333333);
InactiveSleepTime = TimeSpan.FromSeconds(1);
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
TouchPanel.EnabledGestures = GestureType.FreeDrag;
}
protected override void Update(GameTime gameTime)
{
TouchCollection touchCollection = TouchPanel.GetState();
foreach (TouchLocation tl in touchCollection)
{
GestureSample gs = TouchPanel.ReadGesture();
if (FirstTimePressed == true)
{
if ((tl.State == TouchLocationState.Pressed))
{
StartTime = gs.Timestamp;
StartPoint = gs.Delta;
FirstTimePressed = false;
}
}
if ((tl.State == TouchLocationState.Released))
{
EndTime = DateTime.Now;
DragTime = (float)(EndTime - StartTime);
EndPoint = gs.Delta;
DragDistance = EndPoint - StartPoint;
DragVelocity = DragDistance / DragTime;
FirstTimePressed = true;
}
}
while (TouchPanel.IsGestureAvailable)
{
GestureSample gs = TouchPanel.ReadGesture();
switch (gs.GestureType)
{
case GestureType.FreeDrag:
break;
}
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
}
}
答案 0 :(得分:0)
如果我是你,我会这样做 - 当用户第一次按下屏幕时,将全局变量TimeSpan time
设置为TimeSpan.Zero
。然后,在每次更新时,将gameTime.ElapsedRealTime
添加到时间变量。当用户发布时,您可以从时间变量中获取时间,这很容易。要获得秒数,请使用:time.totalSeconds
。
我已经修改了你的代码了,你走了:
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
float DragVelocity, DragDistance, DragTime;
TimeSpan time;
Vector2 StartPoint, EndPoint;
bool isThisFirstTime = true;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
TargetElapsedTime = TimeSpan.FromTicks(333333);
InactiveSleepTime = TimeSpan.FromSeconds(1);
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
TouchPanel.EnabledGestures = GestureType.DragComplete | GestureType.FreeDrag;
}
protected override void Update(GameTime gameTime)
{
while (TouchPanel.IsGestureAvailable)
{
GestureSample gs = TouchPanel.ReadGesture();
switch (gs.GestureType)
{
case GestureType.FreeDrag:
if (isThisFirstTime == true)
{
time = TimeSpan.Zero;
StartPoint = gs.Position;
isThisFirstTime = false;
}
else
{
EndPoint = gs.Position;
time += gameTime.ElapsedGameTime;
}
break;
case GestureType.DragComplete:
isThisFirstTime = true;
DragTime = (float) time.TotalSeconds;
DragDistance = Vector2.Distance(StartPoint, EndPoint);
DragVelocity = DragDistance / DragTime;
break;
}
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
}
}