-1

I have an app with three buttons. When a user taps multiple buttons I would like to have a function that uses the multiple inputs. So for instance, if the buttons have a value of 1, 2, 3 and the user presses button 2 and button 3, I would like to be able to use my event listener to say "the user made 5". I thought that I could use separate event handlers maybe, but then I wouldn't know how to combine the values. I'm a beginner so any help would be appreciated.

Edit: ? How do I get down voted? I researched plenty and there is alot of information on multi-touch, but not on handling multi-touch events at the same time. If there is some I would be more than happy to read it, but just down voting my question because you think it's easy or simple-minded or I haven't researched enough? I've been trying to figure it out since 9am this morning, that's 8 hours for me and at a friends suggestion I'm just asking for a bit of help. Ironically, this is the first time I have ever asked for help here or anywhere and I'm immediately given a cold shoulder.

  • Hey, the people that down vote you are not giving you a cold shoulder. They are simply reacting to a question that does not comply with the required standard as given here: http://stackoverflow.com/help/on-topic - but do not fear. Could you include a little about what you tried? Could you show us the code you tried with event handlers? When it comes to programming never program for a final result, break down you problem. First try and get a single button to post the data. Then add one more. Then see if you can get them to add their values together when pushed. – Nicklas Kevin Frank Jul 17 '14 at 05:26

1 Answers1

0

First of all you will have to have multitouch enabled. Make one listener for all of these buttons. One of ways doing it: Create table that will hold currently pressed buttons. On event.phase == "began" add button to table, on event.phase == "ended" remove it. Then when you want data to be processed, iterate through table and get values/actions or whatever you like from buttons that have their entries in table.

local pressedButtons = {}

function touchListener( event )
    if event.phase == "began" then
        pressedButtons[event.target] = true
    end
    if event.phase == "ended" then
        pressedButtons[event.target] = nil
    end
end

for i = 1, 3 do 
    local button = display.newRect( 70 , i * 80 , 40 , 40 )
    button.value = i
    button:addEventListener("touch", touchListener)
end

function printOutput()
    local sum = 0
    for k,v in pairs(pressedButtons) do
        sum = sum + k.value
    end
    print( sum )
end

timer.performWithDelay( 1000 , printOutput, -1)

Note that instead of value you can assign for example some function that you will call in printOutput funcion. Also note that I am adding button object as key to table for easy removal. Additionally you might want to make touch logic more complex (adding focus etc.) Also I recommend using table listeners.

Piotr
  • 671
  • 6
  • 17