0

I'm making a pairs game. The users can click and match pairs which is working great. However, I'd like to add a timer. I'm not sure how to do this.

I already have a button "start" which is used to start and reset the game. I've added a field "counter" which I'll use to display the time. I'm just not sure how to make a timer work without blocking the game play.

2 Answers2

3

Considering this is a game I would expect you're wanting to display the seconds and end the game after a certain period of time.

LiveCode has great syntax for this. The 'send' command lets you send messages in a designated amount of time. For example:

send "handlerName" to target in 1 second

So applying this principle allows us to create timers in a couple of lines of script

local sSeconds
on countSeconds
   add 1 to sSeconds
   send "timerIncrease" to me in 1 second
end countSeconds

This example will count forever so might not be that useful!

You described a simple game so I would imagine you want to count down from say 60 seconds to 0. When you hit 0, tell the user their time is up.T In your button you could try the following script

local sGameSeconds
local sTimerRunning

on mouseUp
   if the label of me is "start" then
      set the label of me to "reset"
      put true into sTimerRunning
      put 60 into sGameSeconds
      send "timerRun" to me in 1 second
   else
      set the label of me to "start"
      put false into sTimerRunning
      put 60 into field "counter"
   end if
end mouseUp

on timerRun
   if sTimerRunning is true then
      subtract 1 from sGameSeconds

      if sGameSeconds < 1 then
         put 0 into field "counter"
         put false into sTimerRunning
         set the label of button "start" to "start"
         timerFinished
      else
         put sGameSeconds into field "counter"
         send "timerRun" to me in 1 second
      end if
   end if
end timerRun

on timerFinished
   answer "Time Up!"
end timerFinished
Benjamin Beaumont
  • 910
  • 1
  • 6
  • 14
  • I must admit Mark, I guessed. I've written similar games as samples stacks and generally did a count down in seconds. If it's not what they want they can comment and I'll try and help them further. Ultimately, answers to LiveCode questions are coming into the public domain which is great, so thanks for answering the question too. – Benjamin Beaumont Mar 07 '14 at 10:22
  • Hi Mark, I'm sorry you feel that way. I have little interest in personal SO ratings. Stack overflow has become such an important developer resource that it's in all our (LiveCode developers) interest to increase the number of questions and answer tagged with LiveCode. Particularly important to provide new users with answer when they are searching online. There is no secrecy, our intent is very transparent. Post the question asked, answer it directly on SO and send a link to the person who wrote into support. If the user needs clarification our hope is that they'll post back to the SO thread. – Benjamin Beaumont Mar 07 '14 at 17:28
  • So far, we've only posted 4 questions so it's hard to see whether in practice this will encourage those asking tech questions in support to engage with SO. But our hope is that they will. – Benjamin Beaumont Mar 07 '14 at 17:29
  • Ben, of course I understand your goal. It is good that you have now explained what you're actually doing. You might want to write this in LiveCode Support's profile description on SO. That will make things clear for everyone. – Mark Mar 07 '14 at 20:00
1

It isn't entirely clear what you are asking. You write that you want a timer, but you don't say what you want to use it for. Neither do you say if you want this timer to actually display the time or if you just want something to happen after a particular amount of time.

Here's a simple way to display the time:

on showTime
  put the long time into fld "Time"
  send "showTime" to me in 100 milliseconds
end showTime

By refereshing the time every 100 milliseconds, the time on display is never more than 1/10th of a second off.

Here's an efficient way to display a timer that only shows hours and minutes. It sends a showTime message just in time and uses a minimum of processing power:

on mouseUp
   if showTime is in the pendingMessages then
      put the pendingMessages into myMsgs
      filter myMsgs with "*showTime*"
      repeat for each line myMsg in myMsgs
         cancel item 1 of myMsg
      end repeat
   else
      showTime
   end if
end mouseUp

on showTime
   set the itemDel to colon
   put the system time into myTime
   put myTime into fld 1
   put item 2 of myTime into myMinutes
   if myMinutes is 59 then
      add 1 to item 1 of myTime
      if item 1 of myTime >= 24 then
         put 0 into item 1 of myTime
      end if
      put "00" into item 2 of myTime
   else
      add 1 to item 2 of myTime
   end if
   convert myTime to seconds
   put myTime - the seconds into myTime
   send "showTime" to me in myTime seconds
end showTime

You can start the timer by clicking on the button containing the mouseUp handler and you can stop it by clicking the same button again.

If this isn't what you need, please explain more.

Mark
  • 2,380
  • 11
  • 29
  • 49