As mentioned earlier, accelerometer events cannot be tested in the simulator. They only work when a game build is uploaded to a device to see the results. The paddle movement will stay within the wall borders of the level across the x axis. To move the paddle, follow the steps:
Below the
dragPaddle()
function, create a new function calledmovePaddle(event)
:function movePaddle(event)
Add in the accelerometer movement using
yGravity
. It provides acceleration due to gravity in the y direction:paddle.x = display.contentCenterX - (display.contentCenterX * (event.yGravity*3))
Add in the wall borders for the level and close the function:
if((paddle.x - paddle.width * 0.5) < 0) then paddle.x = paddle.width * 0.5 elseif((paddle.x + paddle.width * 0.5) > display.contentWidth) then paddle.x = display.contentWidth - paddle.width * 0.5 end end