Game Development Community

Dealing with Multiple Key Presses...

by Matt Van Gorkom · in Torque Game Builder · 03/29/2005 (4:07 pm) · 10 replies

How can I deal with multiple key presses?
I'm using flags to indicate when a key is down and when it is released. In my movement function I use a couple if statements to catch which keys are down.
I figure that ought to work, but when I hold down more than one key, the previous key is considered let up.
How is everyone else getting around this?

#1
03/29/2005 (4:15 pm)
This sounds like a code bug. It sounds like you're reseting the flag each time you update it.

Post the appropriate code, so that it can be debugged.
#2
03/29/2005 (4:23 pm)
Also keep in mind that due to the way keyboards are scanned, certain keys can suppress other keys. This is a hardware issue, not related to Torque at all.
#3
03/29/2005 (4:25 pm)
The functions called by the actionMap:
function playerUp( %player, %vel )
{ // %player = which player, %vel = velocity of movement
	
	$player1.yAccel = true;
	movePlayer( $player1, -27 );
	$player1.playAnimation(chibi1StandBack);
}

function playerUpStop( %player, %vel )
{
	$player1.YAccel = false;
}

My movement function. It'll be changed later to deal in polar velocities, but for now:
function movePlayer( %player, %vel ) // change to polar angle no %xyVel
{
	// deal with y accel -----------------
	if( %player.YAccel == true && %player.getLinearVelocityY() == 0 )
		%player.setLinearVelocityY( %vel );// change to polar velocity
	else if( %player.YAccel == false && %player.getLinearVelocityY() != 0 )
			%player.setLinearVelocityY( 0 );
			
	
	// deal with x accel -----------------
	if( %player.XAccel == true && %player.getLinearVelocityX() == 0 )
		%player.setLinearVelocityX( %vel );// change to polar velocity
	else if( %player.XAccel == false && %player.getLinearVelocityX() != 0 )
		%player.setLinearVelocityX( 0 );

	
	if( %player.XAccel == true || %player.YAccel == true ) // if player still moving then keep 'em going
		schedule( 50, 0, "moveplayer", %player, %vel );
}

EDIT:
Currently the only keys I'm using are the wsad keys for movement and space for fire.

EDIT2:
Updated with almost working code

EDIT3:
OK, code works, but seems a bit flaky sometimes
#4
03/29/2005 (4:41 pm)
This sounds like the problem i've been having too. I still haven't found a solution.
#5
03/30/2005 (7:42 am)
So can anyone help us out with this? It looks to me like it ought to work.
#6
03/30/2005 (7:49 am)
If you're issue is that when you release the up key you stop moving left/right
This looks like it could be your problem.
function playerUpStop( %player, %vel )
{   
     $player1::YAccel = false;   
     movePlayer( $player1, "0 -27" );
}

Your XAccel may still be true, but your xVel is now 0.
#7
03/30/2005 (8:03 am)
Yeah you might want to rework it a bit so oyu just check the flags in your move function and assign movement as appropriate. I can confirm that what you want to do at a base level works fine though as I just run a small test. Holding one key then pressing another does not cause the previous key to be rleased so that works fine in the engine.
#8
03/30/2005 (9:52 am)
OK, I almost have it. I'll post the corrected code above when I get it right, but now I have another problem. A lack of understanding TorqueScript problem:

How do I access the XAccel and YAccel of the player passed to the function? I get nothing from using %player::XAccel, but of course $player1::XAccel works fine.

function movePlayer( %player, %vel ) // change to polar angle no %xyVel
{

	echo( %player::XAccel SPC %player::YAccel ); // gives nothing
	echo( $player1::XAccel SPC $player1::YAccel ); // gives correct values
}
#9
03/30/2005 (10:04 am)
In point of fact while $playerr::blah works, it isn't standard notation for TorqueScript (Its more of the form you would normally use on static vars in other languages).

The correct syntax is $player.xAccel, or %player.xAccel
#10
03/30/2005 (10:09 am)
Oh, thanks. I still don't get all that stuff yet :P

EDIT:
Changed my code above to current code.
Even closer to working now, but for some reason the player stutters a lot and often goes the wrong way. It rather odd and I don't know why it'd do that.x

EDIT2:
I put in checks so the player only moves if it wasn't moving and stops moving only if it was moving.
Still a bit flaky, but it's the best I've come up with so far and I even made use of a schedule statement :)
I updated the code above.