Left/Right Arrow keys added to Star Explorer

I just finished building the Astroid game tutorial and loved.

I’ve been tying with adding left right arrow key controls for the desktop version and could not get it to work.

Any ideals?

Thanks in advance. 

local function dragShip( event )

local ship = event.target

local phase = event.phase

if ( “began” == phase ) then

– Set touch focus on the ship

display.currentStage:setFocus( ship )

– Store initial offset position

ship.touchOffsetX = event.x - ship.x

elseif ( “moved” == phase ) then

– Move the ship to the new touch position

ship.x = event.x - ship.touchOffsetX

elseif ( “ended” == phase or “cancelled” == phase ) then

– Release touch focus on the ship

display.currentStage:setFocus( nil )

end

return true  – Prevents touch propagation to underlying objects

end

Touch events and key events are different. Your function above is for handling touch events. While they have similar behaviors, you would need a function that handles “key” events.  The touch function moves the ship to where the touch event happens. Key events won’t have a .x and .y to move to. Instead you’re going to get a keyName of “left” or “right” (or “w” and “d” if you’re going to program the “wasd” keys. And you get phases like “up” and down".

If you want to have continuous movement instead of having to repeatedly press the keys, you will also have to get an Runtime enterFrame listener involved.

This tutorial should help and it has an example of using Key events towards the bottom:

https://coronalabs.com/blog/2014/02/04/tutorial-continuous-actions-in-corona/

Rob

Touch events and key events are different. Your function above is for handling touch events. While they have similar behaviors, you would need a function that handles “key” events.  The touch function moves the ship to where the touch event happens. Key events won’t have a .x and .y to move to. Instead you’re going to get a keyName of “left” or “right” (or “w” and “d” if you’re going to program the “wasd” keys. And you get phases like “up” and down".

If you want to have continuous movement instead of having to repeatedly press the keys, you will also have to get an Runtime enterFrame listener involved.

This tutorial should help and it has an example of using Key events towards the bottom:

https://coronalabs.com/blog/2014/02/04/tutorial-continuous-actions-in-corona/

Rob