在xna的点球比赛中,我如何只计算1个进球然后让时间进球?

时间:2012-04-26 05:18:07

标签: c# xna xna-4.0

我在xna中编写了一个惩罚性的游戏,我的问题是我如何只计算1个目标,然后让时间为目标消息?,

我已经解决了守门员,球门和球门区之间的碰撞,我保持并得分,但是当球触及球门区时,当球触及球门区时球的得分总是增加,我怎么算1目标,然后显示目标消息??

这是我到目前为止所做的,我认为有一段延迟,但它不起作用,请帮助我被困在这个

 if (_gameState == GameState.Playing)
         {

             if (CollideGoalArea())
             {
                 if (CollideGoalKeeper())
                 {
                     _messageToDisplay = "Goalie defends the goal!";
                     this.Window.Title = "Missed!";
                     _gameState = GameState.Message;
                 }
                 else
                 {
                     score++;
                     _messageToDisplay = "GOAL!";

                     this.Window.Title = "Goal!";

                     _gameState = GameState.Message;
                 }
             }
             else
             {
                 _messageToDisplay = "You missed the goal.";
                 _gameState = GameState.Message;
             }



         }
         else if (_gameState == GameState.Message)
         {

             if (Mouse.GetState().RightButton == ButtonState.Pressed)
             { // right click to continue playing
                 _gameState = GameState.Playing;
                 balonpos.X = 300;
                 balonpos.Y = 350;

             }

         }

1 个答案:

答案 0 :(得分:3)

问题正在发生,因为只要检测到球在目标内,您的代码就会递增score。由于(默认情况下)Update每秒被调用60次,如果你的球在目标内,则每秒会增加60。

您可以重写代码,以便您的游戏有两种状态:显示消息状态和播放状态:

enum GameState
{
    Playing,
    Message
}

GameState _gameState = GameState.Playing;

String _messageToDisplay = "";
int _score = 0;

protected override void Update(GameTime gameTime)
{
    if(_gameState == GameState.Playing)
    {
        if(CollideGoalArea())
        {
            if(CollideGoalkeeper())
            {
                _messageToDisplay = "Goalie defends the goal!";
                _gameState = GameState.Message;
            }
            else
            {
                _messageToDisplay = "GOAL!";
                score++;
                _gameState = GameState.Message;
            }
        }
        else
        {
            _messageToDisplay = "You missed the goal.";
            _gameState = GameState.Message;
        }
    }
    else if(_gameState == GameState.Message)
    {
        if(Mouse.GetState().RightButton == ButtonState.Pressed) // right click to continue playing
             _gameState = GameState.Playing;

        //you should also reset your ball position here
    }
}

这样,每当球进入球门区域时,它将是守门员的得分,失误或命中。然后,游戏将立即改变状态,以便在您用鼠标右键单击之前不再检查这些条件。

您还可以将输入检测逻辑和球和守门员位置更新逻辑放在这些内容中(您可能不希望玩家在显示消息时能够射击另一个球)。