So I have a problem and I will try my best to describe it.
Here is my code:
-- this function moves the player local function movePlayer( event ) local key = event.keyName if "down" == event.phase and paused == false then if key == "escape" then escGame() end if key == "d" then down = true done = false dirX = speed elseif key == "a" then down = true done = false dirX = -speed elseif key == "w" then down = true done = false dirY = -speed elseif key == "s" then down = true done = false dirY = speed end elseif event.phase == "up" then down = false done = false dirX = 0 dirY = 0 player.angularVelocity = 0 local vx, vy = player:getLinearVelocity() player:setLinearVelocity( 0, 0 ) end end Runtime:addEventListener( "key", movePlayer ) -- move player by applying linear impulses to it local function movePlayer2( event ) playerText.x = player.x playerText.y = player.y - player.height if down == true and done == false then -- move the player done = true print("DIRX : "..dirX.." DIRY : "..dirY.."") if dirX ~= 0 and dirY ~= 0 then dirX = dirX / 2 dirY = dirY / 2 end player:applyLinearImpulse( dirX, dirY, player.x, player.y ) end -- send update moves to other player --[[local move = { code = "playerUpdate", x = player.x, y = player.y, pID = pID } appWarpClient.sendUpdatePeers( json.encode( move ) ) playerx = player.x playery = player.y--]] end Runtime:addEventListener( "enterFrame", movePlayer2 )
and here is a video describing the problem:
https://www.youtube.com/watch?v=7vOIHNCUKJs&feature=youtu.be
I have variable “speed” which is 4. The player gets linear impulses. For example if the W button (on the keyboard) is pressed down the dirY = -speed and then it will start go up because it gets linear impulses. And same thing for all 4 directions.
But however there is a problem. When I press; for instance, W and after second D the character goes faster up. But if I manage to press W and D at the exact same time it goes slower.
So the player goes with constant speed up, down, left, and right, but does not go upright, upleft, updown, opleft.
In my code I tried to make it so that dirX and dirY would be smaller if 2 keys are pressed same time:
if dirX ~= 0 and dirY ~= 0 then dirX = dirX / 2 dirY = dirY / 2 end
But it only works if two buttons are pressed exactly the same time.
I hope that anyone could give me answer to this. Thank you a lot for your time.