3 Replies Latest reply on May 30, 2007 6:34 AM by James Newton, ACP

    Time Problem

    wgb14 Level 1
      Hi all,

      I have the following parent script (I am tryign to implement a stop watch)

      property pCurrentTime, pStartTime

      on new me
      return me

      -- Reset the timer to zero
      on resetTimer me
      set pCurrentTime = 0
      end resetTimer

      -- Start the timer running
      on runTimer me
      set pStartTime = the ticks/60 --ticks (1 tick = 1/1000 of a second)
      end runTimer

      -- Stop the timer (assumes timer has been reset and started)
      on stopTimer me
      set pCurrentTime = the ticks/60 - pStartTime

      end stopTimer

      -- Report the timer's value (assumes timer has been stopped)
      on reportTimer me
      return pCurrentTime
      end reportTimer


      I have two buttons (one start) and one (stop) with the following scripts
      Start button

      on mousedown

      set timer1 = new (script "timer")

      resetTimer (timer1)
      runTimer (timer1)



      on mousedown

      set timer1 = new (script "Timer")

      put "Total Time" && reportTimer(timer1) && "seconds" into member("Time")


      The timer is working but the problem is that it shows me the total time that my computer is on and not the time that has elapased from the moment that i pressed the start button. Any idea what i am doing wrong?

      Many thanks

        • 1. Re: Time Problem
          Doc Raman Level 1
          On your stop button you are creating a new instance of the timer, so it has no connection to the one you created with the start button (sorry if my terminology isn't right...I'm no master of this parent script stuff).

          If you do a little debugging with a few put statements (with a me thrown in there) in the timer script you'll see

          -- "new timer"
          -- "resetTimer <offspring "timer" 3 4b1d2c0>"
          -- "runTime <offspring "timer" 3 4b1d2c0>"
          -- "new timer"
          -- "stopTimer <offspring "timer" 3 4b67830>"
          -- "reportTimer <offspring "timer" 3 4b67830>"

          The start button resets and runs, but the stop button stops and reports from a different instance (or parent?).

          I could easily do this with behaviors, but am weak on parents so am not 100% how to access those sort of things without using globals (which i stopped using when Dir6 came out :)

          Also, I tend to use 'the milliseconds' or _system.milliseconds now, rather than ticks (personal preference).

          I have a feeling many others are better at answering this one...
          • 2. Time Problem
            Chunick Level 3
            Here's a timer object that I had created for another project that would work perfectly:

            • 3. Re: Time Problem
              James Newton, ACP Level 3
              Instead of...

              set timer1 = new (script "timer")

              ... try:

              set timer1 = script "timer"

              This means that the calls will be sent directly to the script itself, rather than an instance of the script. Both buttons will therefore by talking to the same object: the "timer' script.

              This also means that you can only ever have one timer running at a time. If you need to have multiple timers running, then you will need to create a separate instance for each timer, using the new() method. You will also have to store each instance in a property or a global variable so that the runTimer() and stopTimer() commands can be sent to the same instance.

              On a different point, you wrote:
              > the ticks/60 --ticks (1 tick = 1/1000 of a second)

              A tick is in fact 1/60 of a second. Use "the milliseconds" if you want value accurate to 1/1000 of a second.