Does anyone know how to constrain an object to a sling like angry birds. I can get it not to go past a distance (50px ) as you can see below, but it gets stuck.
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
-- Make body type temporarily "kinematic" (to avoid gravitional forces)
event.target.bodyType = "kinematic"
-- Stop current motion, if any
event.target:setLinearVelocity( 0, 0 )
event.target.angularVelocity = 0
elseif t.isFocus then
if "moved" == phase then
--Lock to a sling radius.
local distanceX = math.abs(90-event.x)
local distanceY = math.abs(220-event.y)
local distance = math.sqrt(distanceX\*distanceX +distanceY\*distanceY)
if distance \>= 50 then
t.x= t.x
t.y= t.y
else
t.x = event.x - t.x0
t.y = event.y - t.y0
end
elseif "ended" == phase or "cancelled" == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
-- Switch body type back to "dynamic", unless we've marked this sprite as a platform
local angle=-math.atan((ball1.y-event.yStart)/(event.xStart-ball1.x))
force = 1200
local vx= force \* math.cos(angle)
local vy= force \* math.sin(angle)
ball1:setLinearVelocity(vx,vy)
end
end
-- Stop further propagation of touch event!
return true
end
Thanks [import]uid: 8192 topic_id: 5841 reply_id: 305841[/import]