Kinetic Scrolling code. Table.remove issues [RESOLVED]

Hi everybody. I wrote some basic kinetic scrolling code, but have a strange problem with removing table records.

I have to record all movements to check what was the last direction before finger release. Everything works ok, but when I try to clean path table after scroll was done, I have an errors. I tried table.remove with for loop, path=nil, but nothing helps.

How to clean this table properly?

[lua]display.setStatusBar( display.HiddenStatusBar )
system.activate( “multitouch” )
local cw, ch = display.contentWidth, display.contentHeight

local pic = display.newImage(“Caspar.jpg”, 0,0, false)
pic.x = cw/2
pic.y = ch/2
local path = {}
local i =1
function kinetic (e)

if e.phase == “began” then
storeX = e.x-pic.x
storeY = e.y-pic.y
eventTime = e.time

elseif e.phase == “moved” then
pic.x = e.x - storeX
pic.y = e.y - storeY

path[i] = {}
path[i].x = e.x
path[i].y = e.y
path[i].timer = e.time
i=i+1

elseif e.phase == “ended” then

local totalTime = path[#path].timer - path[#path-4].timer
local pathX = path[#path].x - path[#path-4].x
local pathY = path[#path].y - path[#path-4].y
local velocityX = math.abs (pathX/totalTime)*2
local velocityY = math.abs (pathY/totalTime)*2

finalX = e.x + pathX*velocityX - storeX
finalY = e.y + pathY*velocityY - storeY

transition.to (pic, {transition=easing.outExpo, y=finalY, x=finalX})

end
end

pic:addEventListener (“touch”, kinetic)[/lua] [import]uid: 117007 topic_id: 23902 reply_id: 323902[/import]

In e.phase==“began”, you should set i=1. As the code stands, i will continually increase!

To destroy the path table, you need to do path[1]=nil, path[2]=nil etc (use a loop) and then path=nil.

Personally, I avoid using #path (etc) as I’ve heard there are “issues” with # in lua. Your variable “i” is already counting the number of entries, so maybe use that?
[import]uid: 84768 topic_id: 23902 reply_id: 96314[/import]

Oh, shame on me. “i” wasn’t reset in the start of the function, I got it. Thanks, mate!

Here is the right code for those who wonder. It is more versatile and also handle the error when you tap just once.
[lua]display.setStatusBar( display.HiddenStatusBar )
local cw, ch = display.contentWidth, display.contentHeight

local pic = display.newImage(“pic.jpg”, 0,0, false)
pic.x = cw/2
pic.y = ch/2
local path = {}
local i=1
function pic:touch (e)

if e.phase == “began” then
i=1
storeX = e.x-self.x
storeY = e.y-self.y
eventTime = e.time

elseif e.phase == “moved” then
self.x = e.x - storeX
self.y = e.y - storeY

path[i] = {}
path[i].x = e.x
path[i].y = e.y
path[i].timer = e.time
i=i+1

elseif e.phase == “ended” then

if #path>4 then
local totalTime = path[#path].timer - path[#path-4].timer
local pathX = path[#path].x - path[#path-4].x
local pathY = path[#path].y - path[#path-4].y
local velocityX = math.abs (pathX/totalTime)*2
local velocityY = math.abs (pathY/totalTime)*2

finalX = e.x + pathX*velocityX - storeX
finalY = e.y + pathY*velocityY - storeY

transition.to (self, {transition=easing.outExpo, y=finalY, x=finalX})

for i=1,#path do
path[i]=nil
end
align (self)
end
end
end

pic:addEventListener (“touch”, pic)[/lua] [import]uid: 117007 topic_id: 23902 reply_id: 96352[/import]