I set up my character to flip its image once the “a” key is pressed in this function:
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 - (screenW / 2) - 10}) character:scale(-1, 1) elseif event.phase == "up" then transition.cancel() character:scale(1, 1) end end if event.keyName == "d" then if event.phase == "down" then transition.to(character, {time = 3000, x = character.x + (screenW / 2) + 10}) elseif event.phase == "up" then transition.cancel() end end end
So, I have this function running on an enterFrame event listener and whenever I press ‘a’ the character glitches out and continuously flips its image as I hold down the “a” key. I understand why this is happening, since it is on an enterFrame it would run the code multiple times. I want to know an alternative to doing this. Thank you.
It looks like this:
https://www.youtube.com/watch?v=_2LDpndH_6k&feature=youtu.be