I have a project where the character moves by pressing w, a, s, d, but, they can only move up, down, left, and right in a straight line. I want to have it set up so that when they press, for example, both a and w, their linerVelocity causes them to move up and sideways at the same time. This is my current code:
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)}) character:setLinearVelocity(-200, 0) elseif event.phase == "up" then --transition.cancel() character:setLinearVelocity(0, 0) end end if event.keyName == "d" then if event.phase == "down" then --transition.to(character, {time = 3000, x = character.x - (display.actualContentWidth / 2)}) character:setLinearVelocity(200, 0) elseif event.phase == "up" then --transition.cancel() character:setLinearVelocity(0, 0) end end if event.keyName == "w" then if event.phase == "down" then --transition.to(character, {time = 3000, y = character.y - (display.actualContentHeight / 2)}) character:setLinearVelocity(0, -200) elseif event.phase == "up" then --transition.cancel() character:setLinearVelocity(0, 0) end end if event.keyName == "s" then if event.phase == "down" then --transition.to(character, {time = 3000, y = character.y + (display.actualContentHeight / 2)}) character:setLinearVelocity(0, 200) elseif event.phase == "up" then --transition.cancel() character:setLinearVelocity(0, 0) end end end Runtime:addEventListener( "key", onKeyEvent )
I am stumped as to how to do this, and I don’t want to use transitions, they were fidgety and are no as smooth as setLinearVelocity(). Any help would be appreciated, thank you.