Character stutters when switching keys to move

Right now, I’ve been able to get the character moving back and forth. But there is a small stuttering problem that occurs when the player suddenly lets go of one key. For example, if you let go of a key and quickly switch to the d key there is a small stutter, where the character stops briefly and resumes movement. How can I get rid of the stutter?

The code is in a picture below, this is my code for the function and I am using Runtime:addEventListener(“key”, onKeyEvent), Thank you. 

This might not be the problem, but you could try putting a small delay in your transitions, to give it time to cancel the previous one before starting the new one.

I see, the problem is, that the person playing the game might want to quickly change directions if a monster is heading toward them.

I’m talking like 25-50ms,the player won’t even notice.

However, for smoother movement and control I would do this in an enterFrame listener. If holding left and xScale > - 1, reduce it slightly. If holding right and xScale < 1, increase it. If holding nothing, move towards whichever is nearer out of -1 and 1.

So, I change it to an “enterFrame” instead of a “key” and add the delays. But I don’t know how to add the delay between two separate keyEvents. Could you show some code as an example please?

To simplify, how would I prevent a transition from happening at the “d” key, for example, after the “a” key transition has finished? Would this be placed in the event.phase == “up”?

I am bringing this back up, because I have another project that I am implementing character movement into. My code is this:

function onKeyEvent( event ) -- Print which key was pressed down/up to the log. --local message = "'" .. event.keyName .. "' is " .. event.phase --print( message ) -- Display the key event's information onscreen. --if event.device then -- message = event.device.displayName .. "\n" .. message --end -- If the "back" key was pressed, then prevent it from backing out of the app. -- We do this by returning true, telling the operating system that we are overriding the key. --if (event.keyName == "back") then -- return true --end -- Return false to indicate that this app is \*not\* overriding the received key. -- This lets the operating system execute its default handling of this key. --return false if event.keyName == "a" then if event.phase == "down" then transition.to(character, {time = 3000, x = character.x - (display.actualContentWidth / 2)}) elseif event.phase == "up" then transition.cancel() end end if event.keyName == "d" then if event.phase == "down" then transition.to(character, {time = 3000, x = character.x + (display.actualContentWidth / 2)}) elseif event.phase == "up" then transition.cancel() end end if event.keyName == "w" then if event.phase == "down" then transition.to(character, {time = 3000, y = character.y - (display.actualContentHeight / 2)}) elseif event.phase == "up" then transition.cancel() end end if event.keyName == "s" then if event.phase == "down" then transition.to(character, {time = 3000, y = character.y + (display.actualContentHeight / 2)}) elseif event.phase == "up" then transition.cancel() end end end Runtime:addEventListener( "key", onKeyEvent )

And again, there are delays with the movement and it is not very smooth, are there other ways to invoke character movement. Perhaps applyForce() or setLinearVelocity()?

This might not be the problem, but you could try putting a small delay in your transitions, to give it time to cancel the previous one before starting the new one.

I see, the problem is, that the person playing the game might want to quickly change directions if a monster is heading toward them.

I’m talking like 25-50ms,the player won’t even notice.

However, for smoother movement and control I would do this in an enterFrame listener. If holding left and xScale > - 1, reduce it slightly. If holding right and xScale < 1, increase it. If holding nothing, move towards whichever is nearer out of -1 and 1.

So, I change it to an “enterFrame” instead of a “key” and add the delays. But I don’t know how to add the delay between two separate keyEvents. Could you show some code as an example please?

To simplify, how would I prevent a transition from happening at the “d” key, for example, after the “a” key transition has finished? Would this be placed in the event.phase == “up”?

I am bringing this back up, because I have another project that I am implementing character movement into. My code is this:

function onKeyEvent( event ) -- Print which key was pressed down/up to the log. --local message = "'" .. event.keyName .. "' is " .. event.phase --print( message ) -- Display the key event's information onscreen. --if event.device then -- message = event.device.displayName .. "\n" .. message --end -- If the "back" key was pressed, then prevent it from backing out of the app. -- We do this by returning true, telling the operating system that we are overriding the key. --if (event.keyName == "back") then -- return true --end -- Return false to indicate that this app is \*not\* overriding the received key. -- This lets the operating system execute its default handling of this key. --return false if event.keyName == "a" then if event.phase == "down" then transition.to(character, {time = 3000, x = character.x - (display.actualContentWidth / 2)}) elseif event.phase == "up" then transition.cancel() end end if event.keyName == "d" then if event.phase == "down" then transition.to(character, {time = 3000, x = character.x + (display.actualContentWidth / 2)}) elseif event.phase == "up" then transition.cancel() end end if event.keyName == "w" then if event.phase == "down" then transition.to(character, {time = 3000, y = character.y - (display.actualContentHeight / 2)}) elseif event.phase == "up" then transition.cancel() end end if event.keyName == "s" then if event.phase == "down" then transition.to(character, {time = 3000, y = character.y + (display.actualContentHeight / 2)}) elseif event.phase == "up" then transition.cancel() end end end Runtime:addEventListener( "key", onKeyEvent )

And again, there are delays with the movement and it is not very smooth, are there other ways to invoke character movement. Perhaps applyForce() or setLinearVelocity()?