我有一个使用 MonoGame 框架的 WP8 项目。我有一些代码可以识别水平和垂直拖动事件并执行操作,但我似乎从未收到这些事件。我确实得到了一个 FreeDrag 手势,但 Delta 总是 NaN。
我在游戏的 Initialize 方法中初始化 TouchPanel.EnabledGestures 如下:
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
TouchPanel.EnabledGestures = GestureType.HorizontalDrag | GestureType.FreeDrag;
}
我有一个检查手势类型的方法,如下所示:
private void CheckUserGesture()
{
while (TouchPanel.IsGestureAvailable)
{
var gesture = TouchPanel.ReadGesture();
switch(gesture.GestureType)
{
case GestureType.DragComplete:
System.Diagnostics.Debug.WriteLine("Drag Complete");
break;
case GestureType.FreeDrag:
System.Diagnostics.Debug.WriteLine("Drag Complete");
break;
case GestureType.HorizontalDrag:
if (gesture.Delta.X < 0)
gameVm.MoveLeft(Math.Abs((int)gesture.Delta.X));
if (gesture.Delta.X > 0)
gameVm.MoveRight((int)gesture.Delta.X);
break;
case GestureType.VerticalDrag:
if (gesture.Delta.Y > 0)
gameVm.MoveDown(Math.Abs((int)gesture.Delta.Y));
break;
case GestureType.Tap:
System.Diagnostics.Debug.WriteLine("Rotating Shape Due To Tap Command");
gameVm.RotateClockwise();
break;
}
}
}
这在 Update 方法中调用:
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
// TODO: Add your update logic here
//CheckTouchGesture();
CheckUserGesture();
gameVm.UpdateGame((int)gameTime.ElapsedGameTime.TotalMilliseconds);
}
我也尝试过使用 TouchState:
private void CheckTouchGesture()
{
var touchCol = TouchPanel.GetState();
foreach (var touch in touchCol)
{
// You're looking for when they finish a drag, so only check
// released touches.
if (touch.State != TouchLocationState.Released)
continue;
TouchLocation prevLoc;
// Sometimes TryGetPreviousLocation can fail. Bail out early if this happened
// or if the last state didn't move
if (!touch.TryGetPreviousLocation(out prevLoc) || prevLoc.State != TouchLocationState.Moved)
continue;
// get your delta
var delta = touch.Position - prevLoc.Position;
// Usually you don't want to do something if the user drags 1 pixel.
if (delta.LengthSquared() < DragTolerence)
continue;
if (delta.X < 0)
gameVm.MoveLeft(Math.Abs((int)delta.X));
else if (delta.X > 0)
gameVm.MoveRight((int)delta.X);
else if (delta.Y > 0)
gameVm.MoveDown((int)delta.Y);
}
}
但同样,增量总是 NaN。
有什么我可能需要初始化的东西吗?我尝试了 EnabledGestures 类型的各种组合,但仍然无法让拖动事件起作用。弗里克也不起作用。
Tap之类的东西还不错。
谢谢