【问题标题】:I want to throw a star in the direction of drag done by user我想在用户拖动的方向上扔一颗星
【发布时间】:2014-03-05 15:14:14
【问题描述】:

我想开发一个游戏,用户将向目标拖动,一旦他拖动,就会向目标投掷一颗星。但是我在这个开发的早期阶段遇到了问题。我无法在角度方向上移动星星。我做了以下代码,但星星随着用户拖动而消失。我不想那样。我希望它如下:如果用户向特定方向拖动,那么星星将朝那个方向抛出;直接不是弯曲的方式。

这是我尝试过的:

       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;

                }

    }

【问题讨论】:

  • 使用vector2起点,当用户点击按钮时获取。然后等到用户解锁按钮并使用vector2获取结束位置。然后获取点之间的角度并朝那个方向发送星星。

标签: c# windows-phone-7 xna


【解决方案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/

【讨论】:

    猜你喜欢
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 2020-04-08
    • 2012-12-09
    • 2022-12-04
    • 1970-01-01
    相关资源
    最近更新 更多