繁体   English   中英

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

[英]XNA for WP7 and multitouch update every frame

我在使用触摸/多点触摸输入时遇到问题。

我想在用户按下的位置(完成任务)绘制一个尺寸为100x100的小矩形,但是我也希望它们随着用户移动手指而移动(这不会发生在atm上)。

除了静止不动的部分外,我的行为也很奇怪,比方说,我先用拇指触摸,然后再用中指触摸。 每个手指下面都有两个立方体,但是如果我移开手指(在这种情况下为拇指),我放在第二个手指(中指)下面的立方体将消失,而拇指所在的那个立方体仍将在那里。 我想这个问题一旦解决就可以解决,只要有动作就可以正确更新。

这是“绘制”和“更新”片段。 任何帮助表示赞赏:

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);
}

如果您想将触摸位置与其位置相关联,我建议您使用词典。 密钥应为TouchLocation.Id

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

我在这方面有点迟了,但好在这里是哪里出了问题以及我如何解决...

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

我想我在编写代码的这一部分时很困(凌晨2点后什么也没有发生...甚至没有好的代码)我不知道为什么我要检查是否按下了位置状态...这就是为什么当我移动时不会移动...确实已按下了第一帧,但是一旦开始移动它便是.Moved ...总而言之,将其移除,如果它像一个吊饰一样工作...

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

总而言之,它的行为符合预期。

干杯!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM