Perhaps you should consider adding acceleration to the variable, which increases when you hold the button down as the force increases, so that the players will move until they reach maximum speed, thus achieving an even feeling.
I added a sudden stop when you release left / right, but I would not recommend this, instead reduce xforce to 0 to have a smooth feeling that also slows down.
pixelx - , ( )
window.addEventListener('keydown', handleKeyDown, true)
window.addEventListener('keyup', handleKeyUp, true)
var maxspeed = 6;
var xforce = 0;
var pixelx = 0;
var direction = 1;
var key_left = false;
var key_right = false;
...
if (key_left)
{
xforce--;
direction = -1;
}
if (key_right)
{
xforce++;
direction = 1;
}
if (xforce > maxspeed)
xforce = maxspeed;
if (xforce < -maxspeed)
xforce = -maxspeed;
if (!key_left && !key_right)
{
pixelx = 0;
xforce = 0;
}
else
{
pixelx += xforce;
}
playerlocationx = playerlocationx + pixelx;
playerdirection = direction;
...
function handleKeyDown(event)
{
if (event.keyCode == 37)
key_left = true;
else if (event.keyCode == 39)
key_right = true;
}
function handleKeyUp(event)
{
if (event.keyCode == 37)
key_left = false;
else if (event.keyCode == 39)
key_right = false;
}