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;

             }

         }
4

1 回答 1

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

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

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

于 2012-04-26T05:33:23.493 回答