Howdy, Stranger!

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

When is the function called?

edited June 2013 in Questions Posts: 82

Hi there,

I bought Codea a couple of days ago, and now I'm trying to understand the workings of Lua by studying the examples. In the multi touch example there's something I don't quite understand:

-- Use this function to perform your initial setup
function setup()
    print("This example tracks multiple touches and colors them based on their ID")
    -- keep track of our touches in this table
    touches = {}
end

function touched(touch)
    if touch.state == ENDED then
        touches[touch.id] = nil
    else
        touches[touch.id] = touch
    end
end

-- This function gets called once every frame
function draw()
    background(0, 0, 0, 255)

    for k,touch in pairs(touches) do
        -- Use the touch id as the random seed
        math.randomseed(touch.id)
        -- This ensures the same fill color is used for the same id
        fill(math.random(255),math.random(255),math.random(255))
        -- Draw ellipse at touch position
        ellipse(touch.x, touch.y, 100, 100)
    end
end

It seems the function touched() stores every new touch in the table 'touches', but where is it called? And even though it's not called anywhere, it's apparantly executed every step.

Comments

  • IgnatzIgnatz Mod
    Posts: 5,396

    touched is a built in event, called automatically when you touch the screen, and it is checked between each call to draw.

    You may find it easier to start with Lua (touch is really part of the extra functionality added by Codea). The wiki page has links, and I have an ebook here

    https://www.dropbox.com/s/qh1e7ft4rvhlpt2/Lua for beginners.pdf

  • SimeonSimeon Admin Mod
    edited June 2013 Posts: 5,054

    touched(touch) is called by Codea whenever a touch occurs, changes state, or ends. It is called once per-state change per-touch.

    So if I put one finger on the screen, Codea might call the touched() method as follows:

    touched( touch ) -- With touch.state == BEGAN
    touched( touch ) -- With touch.state == MOVING
    touched( touch ) -- With touch.state == MOVING
    ... MOVING sent for every time the touch changes position, even slightly
    touched( touch ) -- ENDED (I lifted my finger)
    

    If I place two fingers on the screen, Codea might call the touched() method as follows:

    touched( t1 ) -- With state BEGAN
    touched( t1 ) -- With state MOVING
    touched( t1 ) -- With state MOVING
    touched( t2 ) -- With state BEGAN
    touched( t1 ) -- With state MOVING
    touched( t2 ) -- With state MOVING
    touched( t1 ) -- With state MOVING
    touched( t2 ) -- With state MOVING
    touched( t1 ) -- With state MOVING
    touched( t2 ) -- With state MOVING
    ... and so on
    touched( t2 ) -- With state ENDED
    touched( t1 ) -- With state MOVING
    touched( t1 ) -- With state MOVING
    touched( t1 ) -- With state ENDED
    

    Because your fingers won't touch the screen at exactly the same time (or lift at exactly the same time) you won't get touch events perfectly timed together.

    Edit: formatting

  • Posts: 82

    Thank you both for the quick reply and clear explanation, I appreciate it. Also, I read the ebook, it was really helpful. I have a little bit experience with programming, so it was quick to understand.

  • IgnatzIgnatz Mod
    Posts: 5,396

    There are Codea-specific tutorials on the wiki (link at top) that will help you too

Sign In or Register to comment.