multitouch

can somebody post a piece of code, wich enables multitouch? i already looked at the example “multitouch”, but i dont understand:

for k,touch in pairs(touches) do 

at http://lua.gts-stolberg.de/generischFor.php i read that it goes through a table. but how? what is the variable “k”? and “touch”?

there is no line that creates the id of the touch

i copy-pasted all parts of this example (except the elipses) but no multitouch… i expected this, but i dont have any idea how to solve this problem.

PS: one extrapoint for the one who postes a piece of code with multitouch, but only a specific numbers of touches at one time (means, the second touch is recognized but the third one isnt)

  1. right so if the table is something like {a=1, b=2} then the code inside the loop is executed twice. The first time k will be a and touch will be 1, and the second time k will be b and touch will be 2

  2. The touch function is called by codea whenever the user touches the screen. Codea generates its own ID for the touch. Inside the touch function you can do touch.id to find out what Id codea generated. The number itself is not very interesting - what’s interesting is that it lets you know that this touch is the same touch as a previous one (which you would have no way of knowing otherwise since for example the x,y positions can change as the user moves their finger)

The example uses the id as the keys for the table. Then when draw is called it finds each id,touch pair and prints ellipses for each one.

  1. the program is probably working and detecting the touches, but if you dont draw the ellioses, how would you know?

well, my programm ( http://maxiking16.posterous.com/find-the-gem-13 ) would work with multitouch, but:

okay one thing is interesting. in my game, the “touched(touch)” function didnt work. so someone told me, this function never gets called (okay, interesting, you said its called by codea itself) he said, i should add this line in the “draw()” function: “touched(currentTouch)”. then it worked. but then it didnt work again, and i tried to make the “touched(touch)” function beeing not called every touch, but every frame (i did this by calling it “ctouched(touch)” instead of “touched(touch)”. and from then it worked.

but i think this was a short-term solution, right? because it doesnt work with multitouch?

Right. Multitouch needs the touched function, and it does get called. You also need a little bit of work to keep track of each touch. I looked through your code, see if this does what you need:

-- States of the chests:
closed = 1
open = 3

function setup()
    <your stuff here>
    
    -- to keep track of multi-touches
    -- this table will map chest numbers (1,2,3) to the touch information
    -- if a chest number is on this table, then it's moving. otherwise it's standing
    -- the touch information is:
    -- the original x,y position of the touch
    -- the touch id
    touches = {}
end

function draw()
    background(0, 0, 0)
    if gameState == began then
        spriteMode(CORNERS)
        sprite("Planet Cute:Chest Closed",T1E1x,T1E1y,T1E2x,T1E2y)
        sprite("Planet Cute:Chest Open",T2E1x,T2E1y,T2E2x,T2E2y)
        sprite("Planet Cute:Chest Closed",T3E1x,T3E1y,T3E2x,T3E2y)
        spriteMode(CENTER)
        gemy = gemy - 4
        sprite("Planet Cute:Gem Blue",gemx,gemy,50)
        
        if gemy < 420 then
            gameState = running
        end
    end
    
    if gameState == running then
        --ctouched(CurrentTouch)
        spriteMode(CORNERS)
        sprite("Planet Cute:Chest Closed",T1E1x,T1E1y,T1E2x,T1E2y)
        sprite("Planet Cute:Chest Closed",T2E1x,T2E1y,T2E2x,T2E2y)
        sprite("Planet Cute:Chest Closed",T3E1x,T3E1y,T3E2x,T3E2y)
        
        noFill()
        strokeWidth(3)
        rect(w - 310,10,300,50)
        
        gemx = T2E1x + 75
        gemy = T2E1y + 100
    end
    
    if gameState == finished then
        spriteMode(CORNERS)
        --ctouched(CurrentTouch)
        if T1state ~= open then
            sprite("Planet Cute:Chest Closed",T1E1x,T1E1y,T1E2x,T1E2y)
        else 
            sprite("Planet Cute:Chest Open",T1E1x,T1E1y,T1E2x,T1E2y)
        end
        
        if T2state ~= open then
            sprite("Planet Cute:Chest Closed",T2E1x,T2E1y,T2E2x,T2E2y)
        else
            sprite("Planet Cute:Chest Open",T2E1x,T2E1y,T2E2x,T2E2y)
            spriteMode(CENTER)
            sprite("Planet Cute:Gem Blue",gemx,gemy,50)
            spriteMode(CORNERS)
            gemy = gemy + 2
            if gemy > 700 then
                gameState = againQuestion
            end
        end
        
        if T3state ~= open then
            sprite("Planet Cute:Chest Closed",T3E1x,T3E1y,T3E2x,T3E2y)
        else
            sprite("Planet Cute:Chest Open",T3E1x,T3E1y,T3E2x,T3E2y)
        end
    end
    
    if gameState == againQuestion then
        --ctouched(CurrentTouch)
        drawAgainQuestion()
    end
end

function touched(touch)
    tx = touch.x
    ty = touch.y
    
    if gameState == running then
        if touch.state == BEGAN then
            if tx > (w - 310) and tx < (w - 10) and ty > 10 and ty < 60 then
                gameState = finished
            end
            
            if tx>T1E1x and tx<T1E2x and ty>T1E1y and ty<T1E2y then
                if not touches[1] then  -- check if we already have a touch for this chest
                    touches[1] = {x=touch.x-T1E1x, y=touch.y-T1E1y, id=touch.id}
                end
            elseif tx>T2E1x and tx<T2E2x and ty>T2E1y and ty<T2E2y then
                if not touches[2] then  -- check if we already have a touch for this chest
                    touches[2] = {x=touch.x-T2E1x, y=touch.y-T2E1y, id=touch.id}
                end
            elseif tx>T3E1x and tx<T3E2x and ty>T3E1y and ty<T3E2y then
                if not touches[3] then  -- check if we already have a touch for this chest
                    touches[3] = {x=touch.x-T3E1x, y=touch.y-T3E1y, id=touch.id}
                end
            end
        elseif touch.state == MOVING then
            if touches[1] and touches[1].id == touch.id then
                T1E1x = touch.x - touches[1].x
                T1E1y = touch.y - touches[1].y
                T1E2x = touch.x + (150 - touches[1].x)
                T1E2y = touch.y + (200 - touches[1].y)
            elseif touches[2] and touches[2].id == touch.id then
                T2E1x = touch.x - touches[2].x
                T2E1y = touch.y - touches[2].y
                T2E2x = touch.x + (150 - touches[2].x)
                T2E2y = touch.y + (200 - touches[2].y)
            elseif touches[3] and touches[3].id == touch.id then
                T3E1x = touch.x - touches[3].x
                T3E1y = touch.y - touches[3].y
                T3E2x = touch.x + (150 - touches[3].x)
                T3E2y = touch.y + (200 - touches[3].y)
            end
        elseif touch.state == ENDED then
            if touches[1] and touches[1].id == touch.id then
                touches[1] = nil  -- this is how to remove the touch from the table
            elseif touches[2] and touches[2].id == touch.id then
                touches[2] = nil  -- this is how to remove the touch from the table
            elseif touches[3] and touches[3].id == touch.id then
                touches[3] = nil  -- this is how to remove the touch from the table
            end
        end
    elseif gameState == finished then
        if touch.state == BEGAN then
            if tx>T1E1x and tx<T1E2x and ty>T1E1y and ty<T1E2y then
                T1state = open
            elseif tx>T2E1x and tx<T2E2x and ty>T2E1y and ty<T2E2y then
                T2state = open
            elseif tx>T3E1x and tx<T3E2x and ty>T3E1y and ty<T3E2y then
                T3state = open
            end
        end
    elseif gameState == againQuestion then
        if touch.state == BEGAN then
            if ty > 300 and ty < 500 then
                if tx > (w2 - 400) and tx < (w2 - 200) then
                    setup()
                elseif tx > (w2 - 100) and tx < (w2 + 100) then
                    close()
                end
            end
        end
    end
end

Oh wow! This is just amazing! Now I understand the whole thing about tables… thank you! :slight_smile: