Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


managing keyboard input

This topic is 5781 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, how do you manage the keyboard input, so that the player sprite doesn''t do things several times? i.e. when you press the "left-arrow" key the player sprite calls a function setAnimation(firstpic, lastpic), to tell the program which pic to blit when walking left. the problem is this function would be called continuous as long as you hold the key, and wouldn''t achieve the expected effect. you could implement "switch" variables to be sure that something is done only once, but I find it a bit annoying. Is there a better and cleaner method? bye chris

Share this post

Link to post
Share on other sites
There are only two basic ways you can handle this.

One would be to set a flag when a key goes down and clear it when it goes up. That way, when processing the key, you could check the flag and not do the action if the flag is already set.

Another would be to simply add a little logic to the functions being called. For example,

if (key == VK_LEFT)

void setDirection(int dir)
// if already going this direction, bail
if (dir == m_dir)

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!