Jump to content



XNA if statements, confusion!

  • You cannot reply to this topic
5 replies to this topic

#1 Andrew Plumridge   Members   -  Reputation: 100

Like
0Likes
Like

Posted 24 February 2012 - 07:34 AM

Hi guys Posted Image

Not sure if this is really an XNA topic or not.

I have made a very simple higher/lower card game, on my way to making a Shoot/Red Dog game hopefully...

The problem I am having is when asked higher or lower if the next cards are all higher they will all display until the next lower card, where as if the cards are higher/lower/higher/lower/higher the program works as expected.

if (Card1guessed == true)
{
	 if (Keyboard.GetState().IsKeyDown(Keys.H))
	 {
	 	 if (ShuffledCards.ElementAt(2).Value > ShuffledCards.ElementAt(1).Value)
			   Card2guessed = true;
	 }
	 else if (Keyboard.GetState().IsKeyDown(Keys.L))
	 {
	 	 if (ShuffledCards.ElementAt(2).Value < ShuffledCards.ElementAt(1).Value)
			   Card2guessed = true;
	 }
}

I do this for each card except changing the if statement to if((Card1guessed == true) && (Card2guessed == true))...

TIA. Posted Image

Ad:

#2 Manabreak   Members   -  Reputation: 108

Like
1Likes
Like

Posted 24 February 2012 - 07:40 AM

I think this is because the IsKeyDown() is true for multiple update cycles in a row.
Try to add a boolean variable to "lock" the input only for the current cycle:

// Outside the update loop:
bool isKeyDown = false;
// In the update loop:
if (Card1guessed == true)
{
	 if (Keyboard.GetState().IsKeyDown(Keys.H) && !isKeyDown)
	 {
		 isKeyDown = true;
		 if (ShuffledCards.ElementAt(2).Value > ShuffledCards.ElementAt(1).Value)
			   Card2guessed = true;
	 }
	 else if (Keyboard.GetState().IsKeyDown(Keys.L) && !isKeyDown)
	 {
		 isKeyDown = true;
		 if (ShuffledCards.ElementAt(2).Value < ShuffledCards.ElementAt(1).Value)
			   Card2guessed = true;
	 }

	 if(Keyboard.GetState().IsKeyUp(Keys.H))
	 {
		 isKeyDown = false;
	 }
}

Game Development Videos - http://www.youtube.com/user/GDVLog

#3 wicked357   Members   -  Reputation: 194

Like
0Likes
Like

Posted 24 February 2012 - 10:34 AM

You missed the usuage of isKeyDown...

you should place the if (!isKeyDown)... after you are declaring it to be true, then do the rest of the if statement

or you could add && !isKeyDown in the if state inside the 3rd nested if statements

#4 Manabreak   Members   -  Reputation: 108

Like
0Likes
Like

Posted 24 February 2012 - 10:46 AM

View Postwicked357, on 24 February 2012 - 10:34 AM, said:

You missed the usuage of isKeyDown...

you should place the if (!isKeyDown)... after you are declaring it to be true, then do the rest of the if statement

or you could add && !isKeyDown in the if state inside the 3rd nested if statements

Ah, you're right, I corrected it. How silly of me. :D
Game Development Videos - http://www.youtube.com/user/GDVLog

#5 blackbook   Members   -  Reputation: 110

Like
0Likes
Like

Posted 24 February 2012 - 02:04 PM

Normally C# keyboard state goes like this.

KeyboardState keyboardState = Keyboard.GetState();

if(keyboardState.IsKeyDown.Keys(Space) && previousKeyboardState.IsKeyUp.Keys(Space)
{
then do stuff.
}

KeyboardState previousKeyboardState = keyboardState;

previousKeyboardState stores the keyboardState from the last update cycle.

You will at least wanna make previousKeyboardState a class field so the previousUpdates keyboard state is persistent in memory.

As update in Xna is called 60 times per second usually this is why you need the previousKeyboardState keyUp check to prevent the if block from being constantly entered upon the key press.

#6 Andrew Plumridge   Members   -  Reputation: 100

Like
0Likes
Like

Posted 25 February 2012 - 10:54 AM

Thanks guys! Much appreciated, not had chance to do anything with it yet.






We are working on generating results for this topic
PARTNERS