Timer for Touch? Counts total seconds of touching?

Hi! :slight_smile:

i’m very much interested to go on further with Rob’s Shape Matching app which is found on this link

http://www.coronalabs.com/blog/2013/01/29/basic-shape-matching-app/

i’d like to ask if its possible to count the total time the shape was ‘being touched and dragged’ before it finally fit/hit the goal?

so for example if i

touch the shape for 5 seconds,

lift my finger for 2 seconds,

touch the shape again and drag it the goal for another 5 seconds

the timer will return 10seconds of total touch time.

i haven’t started coding yet, as i am still reading some of the timer related functions. but what im thinking is to make an eventlistener which is triggered by touch, and get the timestamp from that. When the eventlistener ends since the finger is lifted, or when the shape is dragged towards the goal, then another timestamp is taken, and from there a mathematical calculation is made to get the delta of the 2 timestamps, thereby getting the amount of time it took.

is this the way to go, or are there better ways to do it? 

thanks a lot!

Ronjo

That sounds about right.  Look at os.time() and os.difftime()

See: http://docs.coronalabs.com/api/library/os/index.html

Cheers.

Will try! Thanks so much!!! :slight_smile: :slight_smile: :slight_smile:

I’d agree with develephant, and add that you’ll need somewhere to store these times you’re working with…

And speaking of the time used… Take a look at the “touch event” structure the sdk passes your code when the user touches your circle – doc is here: http://docs.coronalabs.com/api/event/touch/index.html

So the touch event the sdk gives you has a field which is the milliseconds since the program launched. I’m not sure why this time standard is useful, other than for finely tuning touches / releases etc. to the millisecond level. It is definitely not the same time standard as what is returned from the os.time() function which is in seconds, not milliseconds - so the two are not interchangeable.

So one factor in deciding which time standard to use could be the level of timing precision you need. Work in seconds good enough? Need millisecond accuracy, or even tenths of a second?

Another thing you might want to do is store object related time values in the object that is being touched itself (since you want to accumulate time across separate touch events, perhaps tally them all up later or as you go). One handy way to keep track of the times, and store them in the display objects themselves while still inside the event handler, is like this

[lua]

local function circleTouch(event)

    if( event.phase == “began” ) then

        event.target.touchStart = os.time()

    elseif(event.phase == “ended” ) then

        event.target.touchEnd = os.time()

        event.target.touchDelta = event.target.touchEnd - event.target.touchStart   – Save off the number of seconds passed from began to ended

        print(" – delta == ", event.target.touchDelta )

    end

end

[/lua]

Didn’t get to test the particular above code snippet, but I’ve set variables through the event.target in the past (it can be handy). You’d likely need to do more in the handler, like adding to the delta across different touch events, etc, but that’s the basic idea.

Doesn’t the touch event already pass in time information?  http://docs.coronalabs.com/api/event/touch/time.html

That sounds about right.  Look at os.time() and os.difftime()

See: http://docs.coronalabs.com/api/library/os/index.html

Cheers.

Will try! Thanks so much!!! :slight_smile: :slight_smile: :slight_smile:

I’d agree with develephant, and add that you’ll need somewhere to store these times you’re working with…

And speaking of the time used… Take a look at the “touch event” structure the sdk passes your code when the user touches your circle – doc is here: http://docs.coronalabs.com/api/event/touch/index.html

So the touch event the sdk gives you has a field which is the milliseconds since the program launched. I’m not sure why this time standard is useful, other than for finely tuning touches / releases etc. to the millisecond level. It is definitely not the same time standard as what is returned from the os.time() function which is in seconds, not milliseconds - so the two are not interchangeable.

So one factor in deciding which time standard to use could be the level of timing precision you need. Work in seconds good enough? Need millisecond accuracy, or even tenths of a second?

Another thing you might want to do is store object related time values in the object that is being touched itself (since you want to accumulate time across separate touch events, perhaps tally them all up later or as you go). One handy way to keep track of the times, and store them in the display objects themselves while still inside the event handler, is like this

[lua]

local function circleTouch(event)

    if( event.phase == “began” ) then

        event.target.touchStart = os.time()

    elseif(event.phase == “ended” ) then

        event.target.touchEnd = os.time()

        event.target.touchDelta = event.target.touchEnd - event.target.touchStart   – Save off the number of seconds passed from began to ended

        print(" – delta == ", event.target.touchDelta )

    end

end

[/lua]

Didn’t get to test the particular above code snippet, but I’ve set variables through the event.target in the past (it can be handy). You’d likely need to do more in the handler, like adding to the delta across different touch events, etc, but that’s the basic idea.

Doesn’t the touch event already pass in time information?  http://docs.coronalabs.com/api/event/touch/time.html