How to make Touch[touch.tapCount] work

Hey everyone. I was looking around and no one seemed to have an answer for this. I’m trying to make a program that counts how many times I tap the screen, but I always get error messages when I try to use it.

Here’s the code


function setup()
    touch={}
      
end
function touched(touch)
        touch[touch.tapCount] = touched
          print(touch.tapCount)
    end

function draw()
    for k,touch in pairs(touch)do
      
    end
    ellipse(400,400,40)
end

Thanks

This will print the amount of times you have touched the screen

function setup()
    taps=0
end

function touched(touch)
    if touch.state==BEGAN then 
        taps = taps + 1
        print(taps)
    end
end

Ps to format your code correctly use:

~~~

code here

~~~

No need for touch = {}, just have the touched function like this:

function touched(touch)
    if touch.state == BEGAN or touch.state == ENDED or touch.state == CANCELLED then
        print(touch.tapCount)
    end
end

@Progrmr235 I added the 3 ~'s before and after your code so it displays correctly.

@Progrmr235, two things:

  1. you need to rename your variables so they don’t interfere with each other

  2. you need to increment the variable, not just reset it every touch.

Here’s an example:

function setup()
    touchCount = 0 -- Name your variable something relevant that doesn't conflict with others
end

function touched(touch)
    if touch.state == BEGAN then -- Do this only once every time you touch the screen
        touchCount = touchCount + 1 -- Increment your value (add one to what it already was)
    end
end

The user and all related content has been deleted.

@NatTheCoder I said how. Hold down on the apostrophe key, and select the far left alternate. Or you can use the ` tag.

`~~~`
`Like this`
`~~~`