【发布时间】:2016-01-27 20:11:59
【问题描述】:
我正在尝试为一个对象实现拖放行为,我需要在触摸被按下时进行注册,并且在稍微移动它之后它应该开始拖动。
不幸的是,我使用的是用 C# 实现的自定义框架,它类似于 XNA。
您将如何在 XNA 上做到这一点?
(适用于安卓平板电脑)。
【问题讨论】:
标签: input xna touch mouse drag
我正在尝试为一个对象实现拖放行为,我需要在触摸被按下时进行注册,并且在稍微移动它之后它应该开始拖动。
不幸的是,我使用的是用 C# 实现的自定义框架,它类似于 XNA。
您将如何在 XNA 上做到这一点?
(适用于安卓平板电脑)。
【问题讨论】:
标签: input xna touch mouse drag
显然这个例子应该被重构以使用输入服务和更好地处理触摸上/下概念,但答案的核心保持不变:
// I don't remember the exact property names, but you probably understand what I mean
public override void Update(GameTime time)
{
var currentState = TouchPanel.GetState();
var gestures = currentState.TouchGestures;
var oldGests = this.oldState.TouchGestures;
if (oldGests.Count == 0 && gestures.Count == 1 && gestures[0].State == Pressed)
{
// Went from Released to Pressed
this.touchPressed = true;
}
else
{
// Not the frame it went to Pressed
this.touchPressed = false;
}
if (oldGests.Count == 1 && gestures.Count == 1 && oldGests[0].State == Pressed && gestures[0].State == Pressed)
{
// Touch is down, and has for more than 1 frame (aka. the user is dragging)
this.touchDown = true;
}
else
{
this.touchDown = false;
}
if (oldGests.Count == 1 && oldGests[0].State == Pressed && gestures.Count == 0)
{
// Went from Released to Pressed
this.touchReleased = true;
}
else
{
// Not the frame it went to Pressed
this.touchReleased = false;
}
this.oldState = currentState;
}
您可能需要稍微调整一下“ifs”,因为我实际上不记得手势在您松开手指后的第一帧是否下降到 0,或者它是否仍然显示 1 个称为“已释放”的手势。测试一下,你就会得到答案!
现在您可以在代码的其他地方使用“this.touchDown”来确定您的对象是否应该移动。
【讨论】: