Limit keypress movement
by Brian Wardle · in Torque Game Engine · 09/25/2005 (6:54 pm) · 8 replies
Hi guys.
In my game the player controls an object with the arrow keys. I want to have each keypress (pressing down and releasing) move the object 1 unit in the game world. I have the movement working but instead of moving 1 unit each keypress, it moves anywhere from 3-5 spaces. I believe what is happening is the processtick function, which sees the updated move structure, gets called so fast it's able to update the move several times just in the fraction of a second it takes to release the key. Does that make sense? As an aside, I'm trying to do all my code in the engine to beoome more familiar with it. Any ideas what the problem may be, or how to fix it?
In my game the player controls an object with the arrow keys. I want to have each keypress (pressing down and releasing) move the object 1 unit in the game world. I have the movement working but instead of moving 1 unit each keypress, it moves anywhere from 3-5 spaces. I believe what is happening is the processtick function, which sees the updated move structure, gets called so fast it's able to update the move several times just in the fraction of a second it takes to release the key. Does that make sense? As an aside, I'm trying to do all my code in the engine to beoome more familiar with it. Any ideas what the problem may be, or how to fix it?
#2
09/26/2005 (11:15 am)
I believe that is already what I'm doing. When a key is pressed the flag is set to 1. The move function then gets called. When the key is released, the flag is set to 0. Problem being that before the key is released and the flag set back to 0, the move gets called several times.
#3
09/26/2005 (1:03 pm)
Could you just set a flag the first time through the onKeyDown procedure to prevent subsequent calls to the move function? And reset the flag with the onKeyUp procedure?
#4
Rather than waiting until the OnKeyUp to reset the flag, reset it as soon as you make the move. If you wait until KeyUp, then processtick can be called several times before they release it, moving several spaces with one keypress. On the flip side of that, if their processtick is going very slow, or they have very fast hands, they could hit it several times and only be moved once because it's reset.
To work around both of these issues you could use a counter of the number of times they have struck the keydown and call the function once for each, decrementing the counter each time.
KeyDown, set counter = counter + 1
--processtick checks counter >= 1
---if yes
----move function called
----set counter = counter - 1
--processtick...
--processtick...
KeyUp, does nothing to counter
09/26/2005 (1:29 pm)
I believe this will do what you are asking:Rather than waiting until the OnKeyUp to reset the flag, reset it as soon as you make the move. If you wait until KeyUp, then processtick can be called several times before they release it, moving several spaces with one keypress. On the flip side of that, if their processtick is going very slow, or they have very fast hands, they could hit it several times and only be moved once because it's reset.
To work around both of these issues you could use a counter of the number of times they have struck the keydown and call the function once for each, decrementing the counter each time.
KeyDown, set counter = counter + 1
--processtick checks counter >= 1
---if yes
----move function called
----set counter = counter - 1
--processtick...
--processtick...
KeyUp, does nothing to counter
#5
Then when the key is released, reset the flag to false during the onKeyUp procedure.
09/26/2005 (2:05 pm)
Just setting a flag the first time onKeyDown is called would prevent the move function from being called more than once. So when onKeyDown is called it checks the flag; if false, then call the move function and set the flag to true. If the flag is true, skip the move function.Then when the key is released, reset the flag to false during the onKeyUp procedure.
#6
09/26/2005 (2:36 pm)
Thanks for the ideas guys. I'll give em a try and see what works.
#7
I suggest placing some echos and seeing what is going on, rather than going off of speculation. ;)
10/02/2005 (10:43 pm)
If you just do it when the key is released you should be set?I suggest placing some echos and seeing what is going on, rather than going off of speculation. ;)
#8
To call ur function only once, just add the following lines
function function_name(%val) //in default.bind.cs (key binding)
{
if(%val)
{
function_name(); // name of function u want to call
}
}
%val = 1, on KeyPress
%val = 0, On KeyReleased
12/25/2005 (9:06 pm)
If u r calling a function to handle something on keyPress, By default that function will be called twice. Because the engine will call that function on two occations, First on KeyPress and second on KeyRelease. To call ur function only once, just add the following lines
function function_name(%val) //in default.bind.cs (key binding)
{
if(%val)
{
function_name(); // name of function u want to call
}
}
%val = 1, on KeyPress
%val = 0, On KeyReleased
Torque Owner Teck Lee Tan