Including a Timer

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
stevewhyte
Posts: 25
Joined: Mon Oct 18, 2010 6:32 pm

Including a Timer

Post by stevewhyte » Thu Dec 13, 2012 3:50 pm

Hi,

I am using the "send" command to run a little timer (counting from 60 down
to 0) as follows:

on openCard
put empty into field id 1008
put 60 into timer
put false into halt
updateTimer
end openCard

on mouseDown
put timer into field id 1008
if the mouseLoc is within the rectangle of button id 1012 then answer
"hello"
end mouseDown

on updateTimer
if halt = false then
put timer into field "timer"
put timer - 1 into timer
if timer < 0 then put true into halt
if halt = false then send "updateTimer" to me in 1 second
end if
end updateTimer

This makes updateTimer unpleasantly recursive but anyway...

The value of the timer is available within the card script that contains the
handler "updateTimer" but I would like

A) the timer to run smoothly when other events occur (in this case,
mouseDown) and

B) to be able to access the value of timer from other objects e.g. buttons
on the same card or on other cards. In essence, I want to run concurrent
processes as you can in Scratch. Is there a way of doing this in LiveCode
without it getting unreasonably complicated?

Here's hoping you can help.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10309
Joined: Wed May 06, 2009 2:28 pm

Re: Including a Timer

Post by dunbarx » Fri Dec 14, 2012 12:34 am

Hi.

There is no way for the scripts you wrote to know what is going on in the others. I mean specifically the variables "halt" and "timer".

This is a quick solution, and not the best, but I am driving now, and cannot do better.

Put this in the card script. You need to give each script access to common variables.

Code: Select all

global timer, halt

on openCard
put empty into field 1
put 60 into timer
put false into halt
updateTimer
end openCard

on updateTimer
   if halt = false then
      put timer into field "timer"
      put timer - 1 into timer
      if timer < 0 then put true into halt
      if halt = false then send "updateTimer" to me in 1 second
   end if
end updateTimer
Craig Newman

Post Reply