• Advertisement

Archived

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

managing keyboard input

This topic is 5508 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
Advertisement
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)
setDirection(LEFT)
..

void setDirection(int dir)
{
// if already going this direction, bail
if (dir == m_dir)
return;
..
}

Share this post


Link to post
Share on other sites

  • Advertisement