I was trying to set up a quick testing ground for sprite animations with input to change animations and such, but I noticed roughly 1 second after a key down event the character’s speed appears to get reduced to about half. If I register a key up event before it occurs, the character continues moving at the original pace.
local physics = require( “physics” )
physics.start()
physics.setGravity( 0, 0 )
local player local vx, vy = 0, 0 local function setAnimation( keyName ) player.animID = keyName player:setSequence( player.animID ) player:play() end local function onKeyEvent( event ) local function updateAnimation() if vy == 0 then if vx < 0 then setAnimation( “left” ) elseif vx > 0 then setAnimation( “right” ) end elseif vx == 0 then if vy < 0 then setAnimation( “up” ) elseif vy > 0 then setAnimation( “down” ) end end end if event.keyName == “up” or event.keyName == “down” or event.keyName == “left” or event.keyName == “right” then if event.phase == “down” then if event.animID ~= event.keyName then setAnimation( event.keyName ) end if event.keyName == “up” then vy = vy - 1 elseif event.keyName == “down” then vy = vy + 1 elseif event.keyName == “left” then vx = vx - 1 elseif event.keyName == “right” then vx = vx + 1 end elseif event.phase == “up” then if event.keyName == “up” then vy = vy + 1 elseif event.keyName == “down” then vy = vy - 1 elseif event.keyName == “left” then vx = vx + 1 elseif event.keyName == “right” then vx = vx - 1 end end end if vx == 0 or vy == 0 then updateAnimation() if vx == 0 and vy == 0 then player:pause() player:setFrame( 2 ) end end player:setLinearVelocity( 50 * vx, 50 * vy ) end function scene:create( event ) player = display.newSprite( imageSheet, sequences ) player.x = display.contentCenterX player.y = display.contentCenterY setAnimation( “down” ) player:pause() player:setFrame( 2 ) physics.addBody( player, “dynamic”, { density = 0, friction = 0 } ) Runtime:addEventListener( “key”, onKeyEvent ) end
The image sheet and other scene code was omitted since I felt it wasn’t relevant to the problem.