Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Reacting to a single touch only?

edited August 2014 in Questions Posts: 216

I have a sliding button class in which I'm using for my game, and you slide one of the four button to the center to choose something, but unfortunately there is a cheat as you can slide several buttons and if you are precise trigger two or more of the buttons and calling the functions associated with them eventually giving the awnser. Is there a way to set it so if you touch the screen once it will only react to that touch and no others?



  • edited August 2014 Posts: 2,043

    Track the touches in a table, and only move the slider when it is touched by the touch associated with it.

    Something like this code

    function setup()
        touches = {}
    function touched(t)
        -- sliders
        for i, slider in pairs(sliders) do
            if slider.touchId = nil and touches[] == nil then 
                slider.touchId =
            if slider.touchId == then
                -- normal slider touchy stuff
                if t.state == ENDED then
                    slider.touchId = nil
        -- Track touches
        if t.state == BEGAN or t.state == MOVING then
            touches[] = t
        elseif t.state == ENDED then
            touches[] = nil

    I think this should work.

  • IgnatzIgnatz Mod
    Posts: 5,396

    @Mr_Ninja - why not set a global variable when a button is touched (eg put the button name or number in the variable), and clear it when the touch stops.

    But when any button is touched, first check if the variable already has a value (ie set by another button). If so, ignore the touch. This way, only one button can ever "own" a touch.

Sign In or Register to comment.