简体   繁体   English

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

[英]XNA for WP7 and multitouch update every frame

I'm having trouble with touch/multitouch input. 我在使用触摸/多点触摸输入时遇到问题。

I want to draw a small rectangle, 100x100 in dimensions, wherever the user presses (mission accomplished) but I also want them to move as the user moves his fingers (that's not happening atm). 我想在用户按下的位置(完成任务)绘制一个尺寸为100x100的小矩形,但是我也希望它们随着用户移动手指而移动(这不会发生在atm上)。

I'm also getting weird behaviour besides the not-moving part, let's say I touch first with my thumb, then with my middle finger. 除了静止不动的部分外,我的行为也很奇怪,比方说,我先用拇指触摸,然后再用中指触摸。 Two cubes appear bellow each finger, but if I remove the finger I place first (thumb in this scenario) the cube under the finger I placed second (middle finger) will disappear and the one where my thumb was will still be there. 每个手指下面都有两个立方体,但是如果我移开手指(在这种情况下为拇指),我放在第二个手指(中指)下面的立方体将消失,而拇指所在的那个立方体仍将在那里。 I guess this issue will solve itself once I get this to update correctly whenever there is movement. 我想这个问题一旦解决就可以解决,只要有动作就可以正确更新。

This are the Draw and Update snippets. 这是“绘制”和“更新”片段。 Any help appreciated: 任何帮助表示赞赏:

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

I would recommend using a Dictionary assuming you want to associate touch locations to their position. 如果您想将触摸位置与其位置相关联,我建议您使用词典。 The key should be the TouchLocation.Id 密钥应为TouchLocation.Id

TouchLocation.Id will remain the same for a given interaction. 对于给定的交互,TouchLocation.Id将保持不变。 There is no guarantee that the order of TouchLocations will be the same from one frame to another, so you have to use their ID and not the order they appear in the collection. 不能保证TouchLocations的顺序从一帧到另一帧是相同的,因此您必须使用它们的ID,而不是它们在集合中出现的顺序。

I'm a bit late on this, but well here is what was wrong and how I solve it... 我在这方面有点迟了,但好在这里是哪里出了问题以及我如何解决...

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

I guess I was sleepy when I wrote this part of the code (nothing good happens after 2 AM... not even good code) I don't know why I was checking if the location state was pressed... that's why it didn't move when I move... first frame it is indeed pressed but once you start moving it it's .Moved ... all in all, removed that if and it works like a charm... 我想我在编写代码的这一部分时很困(凌晨2点后什么也没有发生...甚至没有好的代码)我不知道为什么我要检查是否按下了位置状态...这就是为什么当我移动时不会移动...确实已按下了第一帧,但是一旦开始移动它便是.Moved ...总而言之,将其移除,如果它像一个吊饰一样工作...

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

All in all now it behaves as intended. 总而言之,它的行为符合预期。

Cheers! 干杯!

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

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