简体   繁体   中英

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).

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 will remain the same for a given interaction. 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.

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...

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

All in all now it behaves as intended.

Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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