My situation is this:
I have a bunch of items drawn on display group that goes far below the screen size. I can scroll this up and down by dragging a finger up and down the screen. This works perfectly today…
Bu the item list has grown lately, and I’ve had to implement a “flick gesture support” so that I can set the list in motion by itself by “flicking” up/down.
I have tried to do this the following way:
In the touch handler I calculate the difference (deltaY) between the two latest y positions (of the finger) in the
“moved” phase (before entering the “ended” phase). This I use to calculate the (auto) scroll speed that I use to scroll the items automatically after the flick gesture.
To slow down the scrolling I simply ad/subtract the scrollSpeed variable with 1 for each frame.
Problem is that this seems to work perfectly in the simulator, but on a device (Android) sometimes the flick gesture does not set the scrolling in motion at all and other times a careful flick creates a speed demon of a scroller…
I have two questions:
- Can anybody see if there are some bad things going on in my code that should result in this behavior?
(code below)
- Is there some built in functionality in Corona to help me with this stuff? I’ve tried to google “corona flick gesture” but there seems to be all questions and not too many answers.
I’m sorry about the many lines, but I’ve just extracted the lines from the main app and simplified it.
display.setStatusBar( display.HiddenStatusBar ) local w = display.contentWidth local h = display.contentHeight -- Misc variables local mainGroup = display.newGroup() local yOffs = 0; local maxYOffs = (h - 4000) local scrollSpeedY=0 local deltaY=0 local lastMoveY=0 -- Paint 30 rectangles with alternating colors downwards a bit for idx = 1, 30 do local cY = idx \* 200 local bgRect = display.newRect(mainGroup, w/2, cY, w, 200) if (idx % 2 == 1) then bgRect:setFillColor(0) else bgRect:setFillColor(0.5 + 0.03\*idx) end end -- Touch handler local touchHandler = function(e) if (e.phase == "began") then scrollSpeedY = 0 deltaY = 0 end if (e.phase == "moved") then -- Calc delta Y, the pixel difference in position between the last move and this move deltaY = e.y - lastMoveY lastMoveY = e.y -- Move main group according to the y-offset mainGroup.y = math.min(0, yOffs + (e.y - e.yStart)) if (mainGroup.y \< maxYOffs) then mainGroup.y = maxYOffs end end if (e.phase == "ended") then yOffs = mainGroup.y scrollSpeedY = deltaY\*2 end return true end local frameRedrawListener = function(e) -- Keep on scrolling if scroll speed is large enough if (math.abs(scrollSpeedY) \> 0.5) then mainGroup.y = math.min(0, yOffs + scrollSpeedY) if (mainGroup.y \< maxYOffs) then mainGroup.y = maxYOffs end yOffs = mainGroup.y if (scrollSpeedY \< 0) then scrollSpeedY = scrollSpeedY + 1 else scrollSpeedY = scrollSpeedY - 1 end end end Runtime:addEventListener( "enterFrame", frameRedrawListener ) Runtime:addEventListener( "touch", touchHandler )