1

我正在使用 xna 处理 Windows 手机游戏......并寻找一种方法来让我的对象移动,同时按住我在屏幕上作为按钮制作的一些矩形......

我已经试过了,但它只是读取点击手势

          foreach (GestureSample gestureSample in input.Gestures)
                {
                    switch (gestureSample.GestureType)
                    {
                      case GestureType.Hold:
                      case GestureType.Tap:
                      Point tapLocation = new Point((int)gestureSample.Position.X, (int)gestureSample.Position.Y);
                      if (rightRectangle.Contains(tapLocation))
                                {
                                        rightTouched = true;
                                        player.Catapult.CurrentState = CatapultState.MoveRight;
                                        playerPositionUpdate.X += 20;
                                        player.catapultPosition.X += 20;
                                        player.Catapult.catapultPosition.X += 20;
                                        player.Catapult.projectilePositionUpdate.X += 20;
                                        if (player.Catapult.catapultPosition.X == player.Catapult.catapultPosition.X + 20)
                                            rightTouched = false;
                                        CenterOnPosition(player.Catapult.Position - catapultCenterOffset);
                                    }
                       break;
                      }
                 }
4

2 回答 2

2

手势是离散事件。您想每帧轮询触摸面板的状态。为此使用TouchPanel.GetState()

有关信息,请参阅使用触控输入 (Windows Phone)

于 2013-08-11T12:25:43.507 回答
1

如上所述,您不应该将GestureSample其用于此目的,因为使用 dragcomplete 和 freedrag 有点棘手。相反,您应该使用TouchPanel.GetState()

TouchCollection touchCollection = TouchPanel.GetState();
        if (touchCollection.Count == 1)
        {
            var touch = touchCollection[0]; 
            switch (touch.State)
            {
                case TouchLocationState.Pressed:
                    if (rectangle.Contains((int) touch.Position.X, (int)  touch.Position.Y))
                    {
                        isMoving = true;
                    }
                    break;
                case TouchLocationState.Moved:
                    if (isMoving)
                    {
                        TouchLocation prevLocation;
                        touch.TryGetPreviousLocation(out prevLocation);
                        if (prevLocation.Position != touch.Position)
                        {
                            Vector2 delta = touch.Position - prevLocation.Position;
                            //offset your rectangle on delta
                        }
                    }
                    break;
                case TouchLocationState.Released:
                    isMoving = false;
                    break;
            }
        }
        else if (touchCollection.Count == 2)
        {
            var touchOne = touchCollection[0];
            var touchTwo = touchCollection[1];
            //pinch logic here
        }    

我想你明白这个想法。

于 2013-08-13T06:31:41.357 回答