I had a few minutes to kill, so I just decided to roughly code what I meant into the code that you posted.
io.output():setvbuf("no") display.setStatusBar(display.HiddenStatusBar) --\> Physicslocal physics = require("physics") physics.start() --physics.setGravity( 0, 9.81) physics.setDrawMode( "hybrid" ) local reboundValue = -190 local ball local block = {} local function onLocalCollision( self, event ) if ( event.phase == "ended" ) then print( self.category .. ": collision ended with " .. event.other.category ) local vx, vy = ball:getLinearVelocity() ball:setLinearVelocity( vx, reboundValue ) end end local background = display.newRect( 160, 250, 500, 500 ) background:setFillColor( 0.7, 0.5, 0.5 ) ball = display.newCircle(160,200, 10) ball:setFillColor( 1, 0, 0 ) physics.addBody(ball, "dynamic", { density=1, friction=0.0, bounce=1, radius=10 }) ball.collision = onLocalCollision ball:addEventListener( "collision" ) ball.category = "ball" for i = 0, 2 do block[#block+1] = display.newRect( 80+i\*80, 240+i\*40, 20, 20 ) block[#block]:setFillColor( 0, 1, 0 ) physics.addBody( block[#block], "static" ) block[#block].collision = onLocalCollision block[#block]:addEventListener( "collision" ) block[#block].category = "block" end function background:touch(event) if(event.phase=="moved")then ball.x = event.x -- use physics to accomplish this. elseif(event.phase=="ended")then local vx, vy = ball:getLinearVelocity() ball:setLinearVelocity( 0, vy ) end end background:addEventListener( "touch" )
I didn’t touch the x movement, or implement the safety checks for resetting the bounce, but this should give you a good idea of what I was talking about above.
As already stated, I’d recommend using physics to move the ball on the x-axis. You could, for instance, see where the player touches the background. If the player touches to the left of the ball, then you apply linear velocity to the left and vice versa.