触摸XNA中的按钮(具有按下,释放,移动状态)

时间:2013-06-09 12:18:56

标签: c# windows-phone-7 xna windows-phone-8 touch

我正在尝试在WP8中使用所有状态(按下,已释放,已移动)制作触摸按钮,但TouchLocationState.Released无效。

这是我的代码:

类变量:

bool touching = false;
int touchID;
Button tempButton;

Button是一个单独的类,其中包含触摸时切换状态的方法。

Update方法包含以下代码:

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是菜单上的按钮列表。

触摸变量为真后的单独循环(在Update方法中)

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

切换按钮状态的代码工作正常,但我想触发操作的代码不是。

location.State == TouchLocationState.Released大多数都是假的。 (即使在我发布触摸后,它的值为TouchLocationState.Moved) 而且它有时会起作用更令人恼火!

我真的很困惑并且坚持了几天。这是正确的方法吗?如果是,那么我哪里错了?或者还有其他一些更有效的方法吗?

1 个答案:

答案 0 :(得分:1)

我终于找到了解决方案。它不需要released

TouchLocationState状态

在此发布。希望它能帮助别人。 谢谢,如果有人在尝试。

重命名类变量:

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

更新方法现在具有以下代码

        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 { }
            }
        }