Still here.
I put the bounce checks only at the top. And I added the controls to move the ball’s “.x” with physics.
I’m having problems though:
1.If the ball hits the side of a block instead of standing still, it vibrates.
2.compared to before the fluidity has improved but I think we can do more.
Here is the new code:
local horizontalSpeed = 400 local reboundValue = -190 local ball local block = {} local background = display.newRect( 160, 250, 500, 500 ) background:setFillColor( 0.7, 0.5, 0.5 ) local function onLocalCollision( self, event ) if ( event.phase == "ended" ) then --print( self.category .. ": collision ended with " .. event.other.category ) if(event.other.category == "ball")then if((ball.y+ball.height\*0.5) \< (self.y+self.height\*0.5))then local vx, vy = ball:getLinearVelocity() ball:setLinearVelocity( vx, reboundValue ) end end end end ball = display.newCircle(160, 200, 10) ball:setFillColor( 1, 0, 0 ) ball.category = "ball" physics.addBody(ball, "dynamic", { density=1, friction=0.0, bounce=1, radius=10 }) ball.collision = onLocalCollision ball:addEventListener( "collision" ) for i = 0, 2 do block[#block+1] = display.newRect( 80+i\*80, 240+i\*40, 20, 20 ) block[#block]:setFillColor( 0, 1, 0 ) if(i==0)then block[#block].height = 200 end physics.addBody( block[#block], "static" ) block[#block].collision = onLocalCollision block[#block]:addEventListener( "collision" ) block[#block].category = "block" end block[#block+1] = display.newRect( 160, 380, 300, 20 ) block[#block]:setFillColor( 0, 1, 0 ) physics.addBody( block[#block], "static" ) block[#block].collision = onLocalCollision block[#block]:addEventListener( "collision" ) block[#block].category = "block" local tmp = display.newRect( 160, 100, 20, 0 ) tmp.anchorY=1 tmp.y = block[2].y-(block[2].height\*0.5) local nowX, nowY = ball.x, ball.y function background:touch(event) if(event.phase=="began")then nowX, nowY = event.x, event.y elseif(event.phase=="moved")then nowX, nowY = event.x, event.y --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" ) local function enterFrame( self ) local vx, vy = ball:getLinearVelocity() --horizontalSpeed if((ball.x \<= nowX+10)and(ball.x \>= nowX-10))then ball:setLinearVelocity( 0, vy ) elseif(ball.x \< nowX)then ball:setLinearVelocity( horizontalSpeed, vy ) elseif(ball.x \> nowX)then ball:setLinearVelocity( -horizontalSpeed, vy ) end end ball.enterFrame = enterFrame Runtime:addEventListener( "enterFrame", ball )