Sign in to follow this  

XNA if statements, confusion!

This topic is 2122 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

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. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Share this post


Link to post
Share on other sites
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:

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[quote name='wicked357' timestamp='1330101259' post='4916249']
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
[/quote]

Ah, you're right, I corrected it. How silly of me. :D

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 2122 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this