Quote:
I've been trying to make a running/dashing for the player, I've managed to do it by holding down a key (which is easy) but I found it difficult if you had to press the two keys in a sequence (pushing right x 2 would make the player sprint), has anyone found a way around this?
thanks
|
Suggest you record the number of keystrokes for a certain key in a certain time (using a procedure in onTimeout() perhaps?), and clear it when the time is up. In my coding, I have an array of 256 keystrokes, all set to 0. When a user presses the key, the corresponding keystroke in the array is increased by one. Repeat, until the reset time is reached (usually, I go for about 500 ms). To detect if the user pressed the key twice, simply do
if (this.keyStrokes[KeyIdentifier] >= 2) { ... }.
Note, this is probably a more complex explanation than Gambet's. Good luck
I'm a bit rusty in my GraalScript, but I created this. It'll give you an idea. Don't critique me on it, I didn't even test it. It should give you an idea though.
PHP Code:
function onCreated()
{
// The array of keystrokes, of course.
this.keyStrokes = new[0xFF];
// How long before the array resets itself.
this.keyStrokeTime = 500;
// Don't touch.
this.keyStrokeLastTime = 0;
}
function onTimeout()
{
// Yadda yadda
// Check if 500 milliseconds have passed since the last reset.
if ((timevar2 - this.keyStrokeTime) > this.keyStrokeLastTime)
{
// Reset!
for (temp.i = 0; temp.i < this.keyStrokes.size(); temp.i++) this.keyStrokes[temp.i] = 0;
this.keyStrokeLastTime = timevar2;
}
// Check for any keystroke you need for values equal to or above 2.
if (this.keyStrokes[/* whatever key code you want to check here */] >= 2)
{
// Dash code here
}
// Yadda yadda
}
function GraalControl.onKeyDown(temp.keyCode, temp.keyString, temp.scanCode)
{
// Adds one keystroke to the array.
this.keyStrokes[temp.keyCode]++;
}