我想在用户拖动方向上抛出一颗星星

时间:2014-03-05 15:14:15

标签: c# windows-phone-7 xna

我想要开发一种游戏,用户将向目标拖动,一旦拖动,就会向目标投掷一颗星星。但是我在这个开发的最初阶段遇到了问题。我无法在角度方向上移动恒星。我做了以下代码,但明星就像用户拖动一样。我不想那样。我想要它如下:如果用户拖动特定方向,那么星星将被抛向该方向;直线而不是弯曲的方式。

这是我试过的:

       private void OnUpdate(object sender, GameTimerEventArgs e)
    {
        TouchPanel.EnabledGestures = GestureType.FreeDrag | GestureType.DragComplete ;

        if (TouchPanel.IsGestureAvailable)
        {
            gesture = TouchPanel.ReadGesture();
        }

            switch(gesture.GestureType)
                {
                    case GestureType.FreeDrag:

                        position = gesture.Position * 1.2f;

                       break;

                }

    }

1 个答案:

答案 0 :(得分:1)

简单:

Vector2 startSwipePos; // When the user starts swiping
Vector2 endSwipePos; // When the user ends swiping, define those two
Vector2 difference = endSwipePos - startSwipePos;
difference.Normalize(); // Get only the direction, you don't have to do this,
                        // you can also make the speed less instead.
Vector2 velocity = difference * (SPEED HERE IN FLOAT);
pos += velocity;

另见:http://xnafan.net/2012/12/pointing-and-moving-towards-a-target-in-xna-2d/