Issues with touch buttons in XNA (Release state to be precise)

Posted by Aditya on Game Development See other posts from Game Development or by Aditya
Published on 2013-06-09T13:06:55Z Indexed on 2013/11/07 16:15 UTC
Read the original article Hit count: 339

Filed under:
|
|
|

I am trying to make touch buttons in WP8 with all the states (Pressed, Released, Moved), but the TouchLocationState.Released is not working.

Here's my code:

Class variables:

bool touching = false;
int touchID;
Button tempButton;

Button is a separate class with a method to switch states when touched.

The Update method contains the following code:

TouchCollection touchCollection = TouchPanel.GetState();

if (!touching && touchCollection.Count > 0)
        {
            touching = true;

            foreach (TouchLocation location in touchCollection)
            {
                for (int i = 0; i < menuButtons.Count; i++)
                {
                    touchID = location.Id; // store the ID of current touch
                    Point touchLocation = new Point((int)location.Position.X, (int)location.Position.Y); // create a point
                    Button button = menuButtons[i]; 

                    if (GetMenuEntryHitBounds(button).Contains(touchLocation)) // a method which returns a rectangle.
                    {
                        button.SwitchState(true); // change the button state
                        tempButton = button; // store the pressed button for accessing later
                    }
                }
            }
        }
        else if (touchCollection.Count == 0) // clears the state of all buttons if no touch is detected
        {
            touching = false;

            for (int i = 0; i < menuButtons.Count; i++)
            {
                Button button = menuButtons[i];
                button.SwitchState(false);
            }
        }

menuButtons is a list of buttons on the menu.

A separate loop (within the Update method) after the touched variable is true

if (touching)
{
    TouchLocation location;
    TouchLocation prevLocation;

    if (touchCollection.FindById(touchID, out location))
    {
          if (location.TryGetPreviousLocation(out prevLocation))
          {
                Point point = new Point((int)location.Position.X, (int)location.Position.Y);

                if (prevLocation.State == TouchLocationState.Pressed && location.State == TouchLocationState.Released)
                {
                       if (GetMenuEntryHitBounds(tempButton).Contains(point))
                              // Execute the button action. I removed the excess 
                }
           }
     }
}

The code for switching the button state is working fine but the code where I want to trigger the action is not.

location.State == TouchLocationState.Released mostly ends up being false. (Even after I release the touch, it has a value of TouchLocationState.Moved) And what is more irritating is that it sometimes works!

I am really confused and stuck for days now. Is this the right way? If yes then where am I going wrong? Or is there some other more effective way to do this?

PS: I also posted this question on stack overflow then realized this question is more appropriate in gamedev. Sorry if it counts as being redundant.

© Game Development or respective owner

Related posts about XNA

Related posts about xna-4.0