managing keyboard input
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
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;
..
}
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;
..
}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement