local move = "" movePlayer = function ( dt ) if (move == "still") then player.x = player.x -- notice here elseif (move == "right") then player.x = player.x + 5 * dt elseif (move == "left") then player.x = player.x - 5 * dt end end onAccel = function ( event ) local x = event.xGravity if (x == 0) then -- notice here move = "still" elseif (x > 0.1) then move = "right" elseif (x < -0.1) then move = "left" end end enterFrame = function ( ) local dt = getDT() -- A function to calculate delta time, not relevant here. movePlayer(dt) end Runtime:addEventListener( "enterFrame", enterFrame ) Runtime:addEventListener("accelerometer", onAccel)
It works as expected except for one thing, even though I created a condition for when the "xGravity = 0" (the device is exactly 90 degrees) the object still keeps moving in its original direction.
To be clearer, when I tilt the device to the right, the object moves to the right, but when I tilt it back, it keeps moving to the right until I tilt it more to the left and then the object starts moving to the left. It never stands still if it was already moving.