Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

I finally found the solution myself. It does not need the released state of the TouchLocationState

Posting it here. Hopefully it'll help others. Thanks if anyone was trying.

The class variables are renamed:

private Point _touchPoint;
private TouchLocation _touchLocation;
private int _touchID;
private Button _selectedButton;
private bool _touched;
private bool _launchEvent;

The update method now has the following code

        TouchCollection touchCollection = TouchPanel.GetState();

        if (!_touched && touchCollection.Count > 0)
        {
            _touched = false;
            _launchEvent = false;

            foreach (TouchLocation location in touchCollection)
            {
                for (int i = 0; i < menuButtons.Count; i++)
                {
                    Button button = menuButtons[i];
                    _touchID = location.Id;
                    _touchPoint = new Point((int)location.Position.X, (int)location.Position.Y);

                    if (GetButtonHitBounds(button).Contains(_touchPoint))
                    {
                        button.SwitchState(true);
                        _selectedButton = button;
                    }
                }
            }
        }
        else if (touchCollection.Count == 0)
        {
            _touched = false;

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

                if (GetButtonHitBounds(button).Contains(_touchPoint) && _launchEvent)
                {
                    OnReleased(i, PlayerIndex.One);
                    _launchEvent = false;
                }
            }
        }

        ///
        // This if statement checks whether the touch is still inside the button area.
        // Then assigns a value of true to the _launchEvent variable.
        //
        // The 'try' block is used because if the first touch is not on button, then the
        // value of the _selectedButton is null and it will throw an exception.
        ///
        if (touchCollection.FindById(_touchID, out _touchLocation))
        {
            if (_touchLocation.State == TouchLocationState.Moved)
            {
                try
                {
                    if (GetButtonHitBounds(_selectedButton).Contains((int)_touchLocation.Position.X, (int)_touchLocation.Position.Y))
                        _launchEvent = true;
                    else
                        _launchEvent = false;
                }
                catch { }
            }
        }
share|improve this answer
    
You posted a lot of code. What exactly did you change to fix this? You should at least describe that in your answer. –  ashes999 Sep 8 '13 at 16:03
    
You should also check if _selectedButton == null instead of try/catch (which can have horrible performance implications), and you should put if bracket statements in curly braces if (condition) { ... } else { ... } –  ashes999 Sep 8 '13 at 16:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.