Jump to content

  • Log In with Google      Sign In   
  • Create Account


xna keyboard issues


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

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

#1 burnt_casadilla   Members   -  Reputation: 423

Like
0Likes
Like

Posted 09 April 2013 - 11:55 PM

I'm having trouble getting my keyboard to do what I want it to do. 

 

this is at the top of my class

 

        KeyboardState currentKey;
        KeyboardState previousKey;

 

and this is in my update

 

currentKey = Keyboard.GetState();

            if (checkKey(Keys.Left))
            {
                ship.shipPos.X -= 4;
            }

            if (checkKey(Keys.Right))
            {
                ship.shipPos.X += 4;
            }

            if (checkKey(Keys.Space))
            {
                addBullet(bulletFirePos);
            }

            previousKey = currentKey;

 

 

this is the checkKey class

 

        public bool checkKey(Keys key)
        {
            return (currentKey.IsKeyDown(key) && previousKey.IsKeyUp(key));
        }

 

The bullets are shooting fine, one bullet every press of the spacebar...although when I shoot the first bullet it stays right in front of the ship and doesn't move...but thats not a keyboard issue lol. The problem I'm having is with moving left and right. I have to repeatedly press the key to get the ship to move and that's not what I want. How do I change the method so that if I just hold the key down the ship moves?


If you see a post from me, you can safely assume its C# and XNA :)


Sponsor:

#2 Andy474   Members   -  Reputation: 658

Like
4Likes
Like

Posted 10 April 2013 - 06:21 AM

public bool IsKeyDown(Keys key)
{
    return (currentKey.IsKeyDown(key)); // this checks if it is down THIS frame
}
public bool IsNewKeyPress(Keys key)
{
    return (currentKey.IsKeyDown(key) && previousKey.IsKeyUp(key)); // this checks if it is down THIS frame and up LAST frame
}

So, your checkKey() function is checking if it is a new key press, E.g. down this frame up last frame, and so holding the key will only result in this event happening once. Hence, you have to press it multiple times.


Edited by Andy474, 10 April 2013 - 06:24 AM.


#3 Jutaris   Members   -  Reputation: 473

Like
1Likes
Like

Posted 10 April 2013 - 11:39 AM

As Andy said above, IsKeyDown is what you actually want in that scenario.

 

If your bullets aren't moving, you're probably not updating their position every frame :)


My Site -- My Music  -- My Ramblings -- My Game

 Basic Flying Rules:Try to stay in the middle of the air. Do not go near the edges of it. The edges of the air can be recognized by the appearance of ground, buildings, sea, trees and interstellar space. It is much more difficult to fly there.

Come join us for some friendly game dev discussions over in XNA Chat!


#4 burnt_casadilla   Members   -  Reputation: 423

Like
0Likes
Like

Posted 10 April 2013 - 09:35 PM

the keyboard solution gave me worked. But it's just the very first bullet being created lol. As soon as I start the game and press space, it stays on the screen and doesnt move, but each bullet after that works fine!!


If you see a post from me, you can safely assume its C# and XNA :)





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS