I checked out the velocity example in the code sharing, I used the code and tried to modify it so that the ball is a physic body, I found that there was a part of the code that told the ball to drop downwards, But I couldn’t find were was this code to delete and make it so that it falls with gravity of the physic engine. Can someone help me identify this section and tell me how to delete it without making the function go wrong? Also there is a small bug in the code whenever you let go of the ball the animation of the square stops moving. I would appreciate that someone told me how to fix it.
Here is what I have so far, It is ready to be copy pasted into a main.lua document:
[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
local line = display.newImageRect (“line1.jpeg”, 300,2)
physics.addBody(line, “static”, {friction=0.3, bounce=0.3})
line.x = display.contentHeight/2.9
line.y = display.contentWidth/0.7
line.rotation =180
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
prevX = ball.x
prevY = ball.y
end
ball:addEventListener(“touch”, startDrag)
Runtime:addEventListener(“enterFrame”, onMoveCircle)
[import]uid: 23689 topic_id: 11666 reply_id: 311666[/import]