WP7和多点触控的XNA每帧更新

本文关键字:XNA 更新 多点 WP7 | 更新日期: 2023-09-27 17:50:57

触摸/多点触控输入有问题。

我想画一个小矩形,尺寸为100x100,无论用户按到哪里(任务完成),但我也希望它们随着用户移动手指而移动(现在还没有发生)。

除了不动的部分,我也得到了奇怪的行为,假设我先用拇指触摸,然后用中指。每个手指下面会出现两个立方体,但如果我移走放在第一位的手指(在本例中是拇指),放在第二位的手指(中指)下面的立方体就会消失,而拇指所在的那个立方体仍然在那里。我想这个问题会解决自己一旦我得到这个更新正确每当有运动。

这是绘制和更新片段。感谢任何帮助:

protected override void Update(GameTime gameTime)
    {
    if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
        this.Exit();
    TouchCollection touchLocations = TouchPanel.GetState();
    i = 0;
    foreach (TouchLocation touchLocation in touchLocations)
    {
        if (touchLocation.State == TouchLocationState.Pressed)
        {
            pos[i] = touchLocation.Position;
        }
        i++;
    }
    base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);
    spriteBatch.Begin();
    for (j = 0; j < i; j++)
    {
        spriteBatch.Draw(whiteRectangle, new Rectangle(((int)pos[j].X - 50), ((int)pos[j].Y - 50), 100, 100), Color.Chocolate);
    }
    spriteBatch.End();
    base.Draw(gameTime);
}

WP7和多点触控的XNA每帧更新

如果你想将触摸位置与其位置相关联,我建议使用字典。键应该是TouchLocation。Id

TouchLocation。对于给定的交互,Id将保持不变。不能保证TouchLocations的顺序从一帧到另一帧是相同的,所以你必须使用它们的ID,而不是它们在集合中出现的顺序。

我有点晚了,但这里是什么是错误的,我是如何解决的…

foreach (TouchLocation touchLocation in touchLocations)
{
    if (touchLocation.State == TouchLocationState.Pressed)
    {
        pos[i] = touchLocation.Position;
    }
    i++;
}

我猜我是困了,当我写这部分代码(没有什么好发生在凌晨2点之后…甚至不是好的代码)我不知道为什么我要检查位置状态是否被按下…这就是为什么我动的时候它不动……第一帧它确实被压住了,但是一旦你开始移动它,它就被移动了……总而言之,去掉这个if,它就像一个魅力…

foreach (TouchLocation touchLocation in touchLocations)
{
    pos[i] = touchLocation.Position;
    i++;
}

总而言之,现在它的行为是预期的。

干杯!