Skip to main content
gamedev.net
🔒 Locked

XNA if statements, confusion!

Started by Andrew Plumridge 4 replies 3.4k views
Original Post
Andrew Plumridge
Andrew Plumridge
Hi guys smile.png

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.

[source]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;
}
}[/source]

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

TIA. smile.png
HarriPellikka
HarriPellikka
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;
}
}
wicked357
wicked357
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
HarriPellikka
HarriPellikka

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.
DarrenHorton
DarrenHorton
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.
Andrew Plumridge
Andrew Plumridge
Thanks guys! Much appreciated, not had chance to do anything with it yet.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.