Thread: Dashing/Running
View Single Post
  #3  
Old 04-27-2009, 08:21 PM
Programmer Programmer is offline
Coder
Programmer's Avatar
Join Date: Jan 2008
Location: -78.464422, 106.837328
Posts: 449
Programmer has a spectacular aura aboutProgrammer has a spectacular aura about
Send a message via AIM to Programmer Send a message via MSN to Programmer Send a message via Yahoo to Programmer
Quote:
Originally Posted by [email protected] View Post
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.0temp.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.keyCodetemp.keyStringtemp.scanCode)
{
  
// Adds one keystroke to the array.
  
this.keyStrokes[temp.keyCode]++;

__________________
- Iᴀɴ Zɪᴍᴍᴇʀᴍᴀɴ
Reply With Quote