I found this code in the code exchange, it might help you, though I am still trying to find out how to make the ball a physic body. I made a transition animation on the left hand side, I encountered a bug that if you do not make contact with the ball the transition stops. If you find out how to make the ball a physic body, it is much appreciated to let me know how to do it,
here is the code:
local physics = require (“physics”)
physics.start(true)
physics.setDrawMode “hybrid”
physics.setGravity(15, 0)
local square = display.newRect( 0, 0, 100, 100 )
square:setFillColor( 255,255,255 )
local w,h = display.contentWidth, display.contentHeight
physics.addBody( square, “static”,{friction=0.3, boucne=0.3})
local loopPhase1
local loopPhase2
loopPhase1 = function()
transition.to( square, { time=1500, loopCount = 10, x=(w-275), y=(h-50), onComplete=loopPhase2} )
end
loopPhase2 = function()
transition.to( square, { time=1500, delay=30, x=(w±275), y=(h-427), onComplete=loopPhase1 } )
end
–to trigger it to start:
loopPhase1()
local screenW, screenH = display.stageWidth, display.stageHeight
local friction = 0.8
local gravity = .9
local speedX, speedY = 0, 0
local prevX, prevY = 0, 0
local ball = display.newCircle( 0, 0, 25)
ball:setFillColor(255, 255, 0)
ball.x = screenW*.5
ball.y = 20
function onMoveCircle(event)
speedY = speedY + gravity
ball.x = ball.x + speedX
ball.y = ball.y + speedY
if( ball.x >= line1 - ball.width*.5 ) then
ball.x = line1 - ball.width*.5
speedX = speedX*friction
speedX = speedX*-1 --change direction
elseif( ball.x <= ball.width*.5) then
ball.x = ball.width*.5
speedX = speedX*friction
speedX = speedX*-1 --change direction
end
if( ball.y >= screenH - ball.height*.5 ) then
ball.y = screenH - ball.height*.5
speedY = speedY*friction
speedX = speedX*friction
speedY = speedY*-1 --change direction
elseif( ball.y <= ball.height*.5 ) then
ball.y = ball.height*.5
speedY = speedY*friction
speedY = speedY*-1 --change direction
end
end
– A general function for dragging objects
local function startDrag( event )
local t = event.target
local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
– Stop current motion, if any
Runtime:removeEventListener(“enterFrame”, onMoveCircle)
– Start tracking velocity
Runtime:addEventListener(“enterFrame”, trackVelocity)
elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
Runtime:removeEventListener(“enterFrame”, trackVelocity)
Runtime:addEventListener(“enterFrame”, onMoveCircle)
end
end
– Stop further propagation of touch event!
return true
end
function trackVelocity()
speedX = ball.x - prevX
speedY = ball.y - prevY +10 – try changing these numbers, the flick might be more or less sensible if you change this number.
prevX = ball.x
prevY = ball.y -5 – same here.
end
ball:addEventListener(“touch”, startDrag)
Runtime:addEventListener(“enterFrame”, onMoveCircle) [import]uid: 23689 topic_id: 1326 reply_id: 42510[/import]