locked
The touch command runs twice. Why is that? RRS feed

  • Question

  • So i have this little piece of code, using the solution posted before.. but i don't know why it runs twice (as if it is taking two touches).
    Can anyone help me?
        TouchColletion touches;
        touches = TouchPanel.GetState();
                touchedPosition = "";
                if (touches.Count > 0)
                {
                    Vector2 touchPosition = new Vector2(touches[0].Position.X, touches[0].Position.Y);
                    touchedPosition = pressedButton(touchPosition);
                    switch (touchedPosition)
                    {
                        case "CloseStoreMenu":
                            _gameState = GameState.Neutral;
                            _gamescreen = GameScreen.MainGame;
                            animationVector = new Vector2(800, 0);
                            break;
                        case "ShopMenu":
                            _gameState = GameState.ShopMenuAnimation;
                            break;
                        default:
                            break;
                    }
                    
                }
                base.Update(gameTime);
            }
    Thursday, November 3, 2011 7:20 AM

Answers

  • If the user taps the screen quickly, touches will contain an event for where the screen was pressed and another for where it was released. You therefore need to test the state of the touch before processing it.

    Instead of the "if(touches.Count > 0)", try a foreach loop something like so...

    foreach(TouchLocation loc in touches)
    {
       if(loc.State == TouchLocationState.Pressed)
    
    { Vector2 touchPosition = new Vector2(loc.Position.X, loc.Position.Y); // etc. etc.
    }
     }

     

     

     

    Thursday, November 3, 2011 11:09 AM