I am programming a penalty shoout game in xna, my question is how do I count just 1 goal and then let time for a goal message??,
I already solve the collison between the goalkeeper, goal and the goal area, and I keep and score but when the ball touched the goal area, the score always increment when the ball touched the goal area, how do I just count 1 goal and then display a goal message??
this is what I have so far, I think about a delay but its not working, please help I am stucked in this
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;
}
}
The problem is happening because your code increments
scoreany time it detects that ball is inside the goal. Since (by default)Updateis called 60 times per second, if your ball is inside the goal score will get incremented by 60 every second.You could rewrite your code so that your game has 2 states: Displaying a message state and playing state:
This way, whenever ball enters the goal area, it will either be a score, miss or hit on the goalkeeper. The game will then instantly change state so that those conditions aren’t checked again until you right click with your mouse.
You could also put your input detection logic and ball and goalie position update logic inside these if’s (you might not want for the player to be able to shoot another ball while the message is being displayed).