Game Development Community

Detecting more than one key pressed down

by Jim Gaczkowski · in Torque Game Builder · 03/21/2006 (12:58 pm) · 4 replies

Hi,
I normally don't ask for fish but I am not finding the answer in any docs and my source code searches have been fruitless.

I have an actionmap that uses the 4 arrow keys to move my player. I would like to set up diagonals such that the up_right diagonal is triggered by holding both the up and right key.

A non-elegant way would be to have the MovePlayerUp() function set $upkeystatus = 1.
This would allow me to have if($upkeystatus){ //move diagonal! in my MovePlayerRight()


I was looking for some sort of isKeyDown function. Does this exist? Is there a better way?
I want to avoid setting 8 different hotkeys for the 8 movements.





Thanks for your time and thought.

-loopyllama

#1
03/21/2006 (4:48 pm)
Hmm, the way I've been going about doing this is:

I have my bound keys / actionmap:
moveMap.bind(keyboard, left, "moveLeft");
moveMap.bind(keyboard, right, "moveRight");
moveMap.bind(keyboard, up, "moveUp");
moveMap.bind(keyboard, down, "moveDown");

function moveLeft(%val)
{
   $moveLeft = %val;
}

function moveRight(%val)
{
   $moveRight = %val;
}

function moveUp(%val)
{
   $moveUp = %val;
}

function moveDown(%val)
{
   $moveDown = %val;
}

and then where I update the player stuff I have:

if ( $moveRight || $moveLeft || $moveUp || $moveDown )
   {
      if ( $moveRight && $moveUp )
      {
         %angle = 45;
      }
      else if ( $moveRight && $moveDown )
      {
         %angle = 135;
      }
      else if ( $moveLeft && $moveDown )
      {
         %angle = 225;
      }
      else if ( $moveLeft && $moveUp )
      {
         %angle = 315;
      } else if ( $moveLeft )
      {
         %angle = 270;
      } else if ( $moveRight )
      {
         %angle = 90;
      } else if ( $moveUp)
      {
         %angle = 0;
      } else if ( $moveDown)
      {
         %angle = 180;
      }
      $player.setLinearVelocityPolar(%angle,($player.speed*$player.speedMult));   
   } else {
      $player.setLinearVelocity("0 0");
   }

*Edit*
I've seen other ways where you simply use the 0 / 1 value of each direction and set the LinearVelocity that way, but it doesn't account for angular movement speed..
#2
03/21/2006 (6:25 pm)
Brilliant!
#3
03/21/2006 (7:09 pm)
Couldn't you just use this:

function moveHero()
{
   %moveX = $moveRight - $moveLeft;
   %moveY = $moveDown - $moveUp;
   
   %vecdir = t2dVectorScale((%moveX SPC %moveY), $hero.runSpeed); // set $hero.runspeed to some value
   $hero.setLinearVelocity(%vecdir);
}

Using your keybinds and functions, plus this code is exactly what I have and I get eight directions.
#4
03/21/2006 (8:22 pm)
I was using that, but then the player seems to move faster on angles (1,1) than on just one axis (1,0). ..really only important if you have 8-direction movement though.