Rapid keystrokes

Started by
7 comments, last by David_Kay 22 years, 5 months ago
Hello, I'm using DInput and well I'm sure this is mentioned somewhere else in the forums but I can't seem to find the post that mentioned about rapid keystrokes so please forgive me. I would like to know of a way to limit the keystrokes ( don't want to move a sprite too fast ) I have this set up so far. if( Input.IsKeyDown( DIK_RIGHT ) && !bRight ) { xPos += 26; bRight = TRUE; } There is a global boolean variable "bRight" which I set to false initially, and here is the second part it is in WinMain - the game loop .. else { if(FAILED( GameLoop() )) break; DWORD End = timeGetTime(); while( (End-Start) < 30 ) End = timeGetTime(); // Sets back to false after 30ish frames have passed bRight = FALSE; } The problem is it still flys across the screen ( the sprite ) .. any help? Thanks in advanced, David Edited by - David_Kay on November 13, 2001 2:16:00 PM
Huh... oh I see..
Advertisement
.....
Put a cruise control on your sprite

On every iteration through your gameloop, do something like:

if (key_right == true) move_right;

Then you think of a speed the sprite needs to have. You remember it in pixels/millisecond. Now, make sure you got a timer that has the time elapsed since last round, multiply it with your speed, and move the sprite that amount.

Then so your loop again...
LOL I like your idea but it is for a tetris game. I only want the sprite to move the width of a block at once then have a pause so it doesn''t just flyyyyyyyy across. or when the block is going down. .. Perhaps add a variable that counts up to 60 and once it hit 60 reset all boolean variables ... any help please?


Huh... oh I see..
I had that trouble at first too (pressing ESC quits the game AND the program, doh!), I solved this by making the action happen when the key was pressed, but then waiting for the key to be released before being allowed to use it again. Dont know if thats the solution you are looking for, but thats how I do it anyway
It sounds like you''re having more of a timing/speed problem than anything else.

The problem with pausing for a number of frames is, unless the framerate is constant from system to system, you can''t be sure of how long that is. So, what might be a 30th of a second on one system could be a 60th on another, or a 10th on a third.

Perhaps you should use some timing code for updating. I believe QueryPerformanceFrequency() will give you a set of ticks (arbitrary time value for a system) per second, and QueryPerformanceCounter() will give you the number of those ticks since the computer was turned on.

So, you could have some code like

//this is up in variable declaration
LARGE_INTEGER time, lastTime, timePerSecond;

//find out how fast
QueryPerformanceFrequency(timePerSecond);

//game stuff here

//somewhere within the game loop
QueryPerformanceCounter(time)
if((time-lastTime)/timePerSecond > TIME_TO_UPDATE)
UpdateGame();

lastTime = Time;

Just an idea, figure out the FPS, use a deviance to control the movement.
I use (and think it would be a good solution for you too) to do what Ibanez suggested and wait for the key to be released before you execute it''s action again.

You could do this in your game code instead of your Input class. In your Input class, maybe you could make a GetLastKeyState() function to read if the key was down the last time you read the data.

I suggest doing this in your game code and not your Input class because then, if somebody holds down the key you can add the effect of the block moving once, then wait a little while, then continue moving without delay until the key is released.

Invader X
Invader''s Realm
Thanks for all the help everyone
Huh... oh I see..

This topic is closed to new replies.

Advertisement