检测拖放和检测长按(XNA或as3)

时间:2016-01-27 20:11:59

标签: input xna touch mouse drag

我正在尝试实现对象的拖放行为,我需要在按下触摸时进行注册,稍微移动它后应该开始拖动。

不幸的是我正在使用在C#中实现的自定义框架,它就像XNA。

你会如何在XNA上做到这一点?

(这是一款Android平板电脑)。

1 个答案:

答案 0 :(得分:0)

显然,这个例子应该重构为使用输入服务和更好地处理上调/下调概念,但答案的核心仍然是相同的:

// 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'代码中的其他地方,以确定您的对象是否应该移动。

相关问题