I have a code where I have a control panel (simplified to a white rectangle in the code below) that I want the user to be able to drag / flick.
I got the drag functionality to work pretty well, but I can’t get the flick working (acceleration caused by the drag) properly.
I think it tracked the problem to the way I calculate the acceleration in the lines:
local flickSpeed = ((localEventY-t.y0) / dT )\*.001
local newYAfterAcelleration = setToWithinLimits((t.y+flickSpeed),lowestDragPos,highestDragPos
But I can’t figure out how to solve it. Flicking down works fine, but unless the user makes a large movement, flick up works downwards.
Any help is appreciated! Thanks.
[code]
–helper function
local function setToWithinLimits(setTo,lowerLimit,upperLimit)
local result
if (setTo > upperLimit ) then result = upperLimit
elseif (setTo < lowerLimit) then result = lowerLimit
else result = setTo
end
return result
end
–objects
local localGroup=display.newGroup()
local panelGroup = display.newGroup()
localGroup:insert(panelGroup)
local panelBG = display.newRect(0,0,200,display.contentHeight*2)
panelBG:setFillColor(255,255,255)
local referenceObject=display.newCircle(20,100,5)
referenceObject:setFillColor(0,255,0)
panelGroup:insert(panelBG)
panelGroup:insert(referenceObject)
–drag function
local function dragPanel( event )
local t = event.target
local phase = event.phase
local highestDragPos = panelBG.contentHeight + display.contentHeight
local lowestDragPos = 0
local initialTimeMsec = system.getTimer()
local localEventX , localEventY = t.parent:contentToLocal(event.x,event.y)
if “began” == phase then
display.getCurrentStage():setFocus( t, event.id )
t.isFocus = true
– Store initial position
t.y0 = localEventY - t.y
initialTimeMsec = system.getTimer()
elseif t.isFocus then
if “moved” == phase then
local newY = localEventY - t.y0
t.y = setToWithinLimits(newY,lowestDragPos,highestDragPos)
elseif “ended” == phase or “cancelled” == phase then
– calc speed
local dT = system.getTimer() - initialTimeMsec
local flickSpeed = ((localEventY-t.y0) / dT )*.001
local newYAfterAcelleration = setToWithinLimits((t.y+flickSpeed),lowestDragPos,highestDragPos)
transition.to(t,{y=newYAfterAcelleration,transition=easing.outExpo})
display.getCurrentStage():setFocus( t, nil )
t.isFocus = false
end
end
return true
end
panelGroup:addEventListener(“touch”,dragPanel)
[/code] [import]uid: 33608 topic_id: 15467 reply_id: 315467[/import]
[import]uid: 3826 topic_id: 15467 reply_id: 58138[/import]