why can’t my function list1:selectFromTouch grab indices 1 - 9?

Update 2

Update 1

  • Fixed the selectFromTouch function thanks to dave
  • Broke up list1:scroll into seperate funcs
  • Created list1:update function
  • Minor tweaks on tweens - added twoif self.tween then tween.stop(self.tween) end blocks in if touch.state==ENDED for consistency
  • Minor clean up on other functions, renamed some variables
  • List Scroll ver2 - Pastebin.com

———————————————-

OP
List1:selectFromTouch - Pastebin.com

it works on the other indices after those. I’m not sure what’s wrong here. I’m also thinking that the indicies I can grab changes depending on how I scroll, since sometimes I struggle to grab 10.

You know it’s a textwall of doom when it’s a pastebin. sorry…

@xThomas - hmmm, curious - why are you using Codea to provide an index of Love2D functions? Aren’t you better setting something up on your Mac/pc to do that, or are you running Love2D on iOS?

@Bri_G I alt tabbed between sublime and the wiki one too many times, remembered how pretty the Codea reference was, and decided to make one for love… To answer your question, I haven’t tried using love2d with iOS yet, still getting the hang of it on PC

As a project it should be pretty easy even for me, I don’t really need to plan it out - easy and fun :smile:

(Although saying that is one thing, figuring out what went wrong with my selection code is another (took so long trying to figure it out, then my battery died :neutral: )

@xThomas Forget my last comment. I deleted it because I didn’t look close enough to what you were doing. I’ll look at it again later.

@xThomas Check the values for your top and bottom variables in the function list1:selectFromTouch. You’re returning from the function in the if statement.

@xThomas Try this function instead of yours. I don’t like the way I calculate index, but it seems to work.

function list1:selectFromTouch(touch)
    index=((HEIGHT-touch.y+self.y)/(HEIGHT/9)+1)//1-9
    self.selected=items1[index]
    self.selected.state = touch.state
    print("selected index: "..index, self.selected.title)
end

After I posted the above code, I was able to change your function to work. See the comments I put in. I didn’t look at your code that close to see why I have to subtract 10 in yours and 9 in mine.

function list1:selectFromTouch(touch)
    local top=self.y
    local bottom=self.y-(#items1*HEIGHT/9)
    self.height=top-bottom

    -- subtract 10 and comment out the if statement.
    local listY=touch.y-(top-self.height)-10
    --if listY>top or listY<bottom then return end
        
    local index=math.ceil(listY/(HEIGHT/9))
    index=#items1-index+1
    self.selected=items1[index]
    self.selected.state = touch.state
    print("selected index: "..index, self.selected.title, "at pos:y - "..listY)
end

@xThomas I’m not sure how you’re doing your scrolling/choice calculations, but here’s how I do scrolling/choice in the getPosition function.

displayMode(FULLSCREEN)

function setup()    
    nbr=40
    w=150
    h=HEIGHT//9
    dy=0
    pos=0
    textMode(CORNER)
end

function draw()
    background(40, 40, 50)
    fill(255)
    text("Tap a box for position",WIDTH/2,HEIGHT-100)
    text("Slide finger up or down for more choices",WIDTH/2,HEIGHT-150)
    for z=1,nbr do       
        fill(59, 110, 122, 255)
        rect(0,HEIGHT-z*h+dy,w,h-5)
        fill(255)
        text(z,60,HEIGHT-z*h+dy+60)
    end
    if pos>0 then
        fill(255,0,0)
        text(pos,WIDTH*.75,HEIGHT/2)
    end
end

function touched(t)
    if t.state==BEGAN and t.x<200 then
        getPosition(t.y)
    elseif t.state==MOVING then
        dy=dy+t.deltaY
        if dy<0 then
            dy=0
        end
    end
end

function getPosition(y) 
    pos=(HEIGHT-y+dy)//h+1
    if pos<1 or pos>nbr then
        pos=0
    end
end

Lots of Thanks @dave1707

Now that I have compared my code to yours, my mistake was indeed the if statement.


That was a emergency bug fix because I would have an error if I scrolled and then tapped before it snapped back. The bug fix causes more bugs :lol:  In hindsight a simple `if not items1[index] then return end` works better and is probably more efficient* :triumph: 


————————

Edit: I updated my code, you can see it in the OP. it’s basically the same, just a bit cleaner

About my (original) scrolling code, the way I’ve coded it is a bit mixed

function list1:scroll()
self.dt = self.dt + 1
if self.deltas[11] then
table.remove(self.deltas, 1)
end
if self.deltas[1] and self.dt % 2 == 0 then
table.remove(self.deltas, 1)
end
self.y = self.y + self.vel.y
self.vel.y = self.vel.y * self.friction
if not self.maxY or not self.minY then return end
if self.y > self.maxY + 32 then
if self.tween then tween.stop(self.tween) end
self.vel.y = 0
self.tween = tween(.25,self,{y=self.maxY},tween.easing.quadOut)
elseif self.y < self.minY - 32 then
if self.tween then tween.stop(self.tween) end
self.vel.y = 0
self.tween = tween(.25,self,{y=self.minY},tween.easing.quadOut)
end
end


Scrolling by using velocity and friction is done with these variables

* dt - timer for the deltas table, increments every tick
* deltas - table holding the last 10 deltaY’s, removes position 1 every other tick
* if deltas has more than 10 positions, we remove position 11
* y, friction, vel represent the list’s position, friction and velocity

Tweens are technically not for scrolling anymore, but if you scroll past the top or bottom of the list, tweens snap it back to the maxY or minY(and also stop tween, ans set vel.y to 0 because having more than one controller for the scrolling is kind of bad) 

if not minY or not maxY... just checks that those exist. I didn’t create those until the touched function runs because I’m scatterbrained



now, in touched is where i actually calculate stuff

every moving touch, deltaY is inserted into the deltas table

when touches end, velocity is set to the average of the last ten deltas and the deltas table is cleared