Game Development Community

Controls and Keybinds

by Steven Sheffey · in TGB Platformer Kit · 10/11/2008 (6:44 pm) · 2 replies

Hey guys. My problem is the key presses continue to run even after the key is released. For example, if I press and hold lshift (jump), then press and release D (right), the Actor will continue to run right even though I released the key. This problem only occurs only when jump is held and will affect all other keys in this way.

Here's the code I have:
if (!isObject(ControllerBehavior))
{
	%template = new BehaviorTemplate(ControllerBehavior);
	
	%template.friendlyName	= "Player Controller";
	%template.behaviorType	= "Actor";
	%template.description	= "Player Controller";
	
	%template.addBehaviorField( keyLeft,	"Move Left",	                KEYBIND,	"keyboard A");
	%template.addBehaviorField( keyRight,	"Move Right",	                KEYBIND,	"keyboard D");
	%template.addBehaviorField( keyUp,		"Move Up",		KEYBIND,	"keyboard W");
	%template.addBehaviorField( keyDown,	"Move Down",	                KEYBIND,	"keyboard S");
	%template.addBehaviorField( keyJump,	"Jump",			        KEYBIND,	"keyboard lshift");
	%template.addBehaviorField( keyAttack,	"Attack",		                KEYBIND,	"keyboard down");
}

/// Set up the player's controller
function ControllerBehavior::onAddToScene(%this, %scenegraph)
{
	if (!%this.Owner.getBehavior(ActorBehavior))
		return;
	
	// Record the controller's id
	%this.Owner.Controller = %this;
	
	// Initialise the direction
	%this.Direction = 0 SPC 0;
	
	// Set the collision details
	%this.Owner.setObjectType("PlayerObject");
	%this.Owner.setCollidesWith("SolidPlatform EnemyObject PlayerTrigger");
	
	// Make sure we have a move map
	if (!isObject(moveMap))
		return;
	
	// Bind the appropriate keys
	moveMap.bindCmd(getWord(%this.keyLeft, 0),		getWord(%this.keyLeft, 1),		%this @ ".keyDown(Left);",			%this @ ".keyUp(Left);" );
	moveMap.bindCmd(getWord(%this.keyRight, 0),		getWord(%this.keyRight, 1),		%this @ ".keyDown(Right);",			%this @ ".keyUp(Right);");
	moveMap.bindCmd(getWord(%this.keyUp, 0),		getWord(%this.keyUp, 1),		%this @ ".keyDown(Up);",			%this @ ".keyUp(Up);" );
	moveMap.bindCmd(getWord(%this.keyDown, 0),		getWord(%this.keyDown, 1),		%this @ ".keyDown(Down);",			%this @ ".keyUp(Down);" );
	moveMap.bindCmd(getWord(%this.keyJump, 0),		getWord(%this.keyJump, 1),		%this @ ".keyDown(Jump);",			%this @ ".keyUp(Jump);" );
	moveMap.bindCmd(getWord(%this.keyAttack, 0),	getWord(%this.keyAttack, 1),	%this @ ".keyDown(Attack);",		%this @ ".keyUp(Attack);" );

	
	// Make sure the controller works
	moveMap.push();
}

/// A key is pressed
function ControllerBehavior::keyDown(%this, %keyDown)
{
	if (!%this.Owner.ActorBehavior.Alive)
			return;
	// Left key
	if (%keyDown $= "Left")
		%this.Direction.X = -1;
	
	// Right key
	if (%keyDown $= "Right")
	//echo ("Moving Left");
		%this.Direction.X =  1;
	
	// Up key
	if (%keyDown $= "Up")
		%this.Direction.Y = -1;
	
	// Down key
	if (%keyDown $= "Down")
		%this.Direction.Y =  1;
		
		
	// Jump key
	if (%keyDown $= "Jump")
	{
		// Note that the jump key is being held
		%this.Jump = true;
		
		// Record the jump Time
		%this.Owner.ActorBehavior.JumpTime = getSceneTime();
		
		// Which type of jump
		if (%this.Direction.Y > 0 && %this.Owner.ActorBehavior.isMethod("jumpDown"))
			%this.Owner.ActorBehavior.jumpDown();
		else if (%this.Owner.ActorBehavior.isMethod("jumpUp"))
			%this.Owner.ActorBehavior.jumpUp();
	}	
}

/// A key is released
function ControllerBehavior::keyUp(%this, %keyUp)
{
	// Left key
	if (%keyUp $= "Left"  && %this.Direction.X == -1)
		%this.Direction.X = 0;
	
	// Right key
	if (%keyUp $= "Right" && %this.Direction.X == 1)
		%this.Direction.X = 0;
		
	// Up key
	if (%keyUp $= "Up")
		%this.Direction.Y = 0;
	
	// Down key
	if (%keyUp $= "Down")
		%this.Direction.Y = 0;
	
	// Jump key
	if (%keyUp $= "Jump")
		%this.Jump = false;
}

#1
10/13/2008 (2:09 am)
Firstly, could you please not post large chunks of the PSK's code, especially entire behaviors ;)

Your issue most likely relates to your keyboard or the fact that your jump key is a "modifier" key (ctrl, alt, shift, etc). I think in this case, what you press is "shift" to jump, then hit "right" to move right, then release "shift-right".

I suspect one of those two things is happening.
#2
07/17/2012 (12:04 pm)
To anyone who finds this thread is search of a solution to this problem, see here for exactly what needs to be done: www.garagegames.com/community/forums/viewthread/122193/