C# 如何测量Windows Phone上的拖动时间?
我想测量阻力的时间。从开始(当玩家触摸屏幕时)到结束(当玩家从屏幕上松开手指时)。 另外,我想测量阻力的距离来计算阻力的速度 但我总是收到以下两条错误消息 1) 当我触摸屏幕时,我在foreach循环的第一行中收到此错误消息:C# 如何测量Windows Phone上的拖动时间?,c#,windows-phone-7,windows-phone-8,xna,C#,Windows Phone 7,Windows Phone 8,Xna,我想测量阻力的时间。从开始(当玩家触摸屏幕时)到结束(当玩家从屏幕上松开手指时)。 另外,我想测量阻力的距离来计算阻力的速度 但我总是收到以下两条错误消息 1) 当我触摸屏幕时,我在foreach循环的第一行中收到此错误消息: GestureSample gs = TouchPanel.ReadGesture(); Microsoft.Xna.Framework.Input.Touch.ni.dll中发生类型为“System.InvalidOperationException”的异常,但未在用
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);
}
}
如果我是你,我会这样做-当用户第一次按下屏幕时,将全局变量
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);
}
}
您不能使用和处理拖放事件吗?哪些拖放事件?我不知道怎么做。它起作用了。但有时,如果我执行很短的拖动,变量DragVelocity没有一个正常值,但它只是表示无穷大。为什么在我执行非常短的拖动时有时会发生这种情况?如何检查值是否为无穷大?如果值为无穷大,我想在屏幕上写一些东西,例如:“请执行新的拖动”。我用下面的代码尝试了一下,但它不起作用:如果(DragVelocity==Infinity)
FreeDrag
手势在固定长度的移动后被检测到,这意味着如果你的拖动太短,游戏将无法检测到它,我认为这会导致DragVelocity=DragDistance/DragTime
其中DragTime
为0。如果你想检查float是否为正无穷大,然后做某事,只需使用以下命令:“if(float.IsPositiveInfinity(DragVelocity))DrawText(“blabla”)检查如何在xna中绘制文本。