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()?