5 Replies Latest reply on Jun 7, 2008 11:15 AM by Chunick

    Problem setting up Timer

    Opera Rat Level 1
      I always seem to have trouble with timers. There is just something missing from my understanding of them. This script goes in a frame. It is supposed to play a sound after 30 seconds and then wait for the user to push the spacebar to move on the user can also hit "a" after 30 seconds to hear the sound again. The spacebar and the "a" key shouldn't work for 30 seconds. I don't understand the object part of the TimeOut. Here is my screwed up script:
      -----------------------------------------
      global gPitch
      property pPitch

      on getPropertyDescriptionList me
      list = [:]
      addProp list, #pPitch, [#Comment: "Select Pitch", #format: #sound, #default: " "]
      return list
      end

      --wait 30 seconds and play sound
      on exitFrame me
      myTimer = timeOut("timer1").new(8000, #playPitch)
      if keyPressed("a") then
      sound(1).play(gPitch)
      go to the frame
      else if keyPressed(SPACE) then
      go to next
      else go to the frame
      end if
      end if

      end


      on playPitch me
      sound(1).play(gPitch)
      end

        • 1. Re: Problem setting up Timer
          Level 7
          > --wait 30 seconds and play sound
          > on exitFrame me
          > myTimer = timeOut("timer1").new(8000, #playPitch)

          Part of your problem might be that every frame you are re-creating the
          timeout object referenced by your local myTimer variable. And 8000 = 8
          seconds, not 30

          Try something like the following:
          --
          property myWaitTime
          property myWaitDone
          property myPitch

          on getPropertyDescriptionList
          pdl = [:]
          pdl[#pPitch] = [#comment: "Select Pitch", #format: #sound, #default:
          member(0, 0)]
          return pdl
          end

          on beginSprite me
          myWaitTime = _system.milliseconds + 30000
          myWaitDone = 0
          end

          on exitFrame me
          if myWaitDone = 0 then
          if (_system.milliseconds > myWaitTime) then
          myWaitDone = 1
          sound(1).play(myPitch)
          end if
          else
          if keyPressed("a") then sound(1).play(myPitch)
          if keyPressed(SPACE) then
          go next
          exit
          end if
          end if
          go to the frame
          end
          • 2. Re: Problem setting up Timer
            Opera Rat Level 1
            Too logical, I love it. (I knew that 30 seconds wasn't 8000, but for testing I shortened it so I wouldn't have to wait so long to see if it worked.) I never knew you could use pld instead of list. I like that; it's clearer. I guess I still don't understand TimeOuts and when to use them.

            Thanks!!
            • 3. Problem setting up Timer
              Chunick Level 3
              Use timeouts in scripts that are not constantly called... a timeout object is like the Ron Popiel Showtime Rotisseries of Lingo, "Set it and forget it!"

              A timeout object runs independently of your other lingo code; when you set the timeout object and it's parameters it immediately begins the timing and calls the handler you specified every xxx milliseconds based off the period you specified. There's really no other magic to it than that.
              • 4. Re: Problem setting up Timer
                Level 7
                Just a small correction, if I may...

                > begins the timing and calls the handler you specified every xxx
                > milliseconds
                ..and calls the handler soon as director is idle, and the timeout of xxx
                milliseconds has expired. Thererfore, you shouldn't expect a timeout to be
                always on time. The 'period' is actualy the desired, or minimum period.

                > A timeout object runs independently of your other lingo code;
                Though these are director's internal stuff, I'd say that a timeout doesn't
                'run'. Timeouts are kept in a list and are checked when director is idle (no
                other script executing).


                "Chunick" <webforumsuser@macromedia.com> wrote in message
                news:g2bg78$b68$1@forums.macromedia.com...
                > Use timeouts in scripts that are not constantly called... a timeout object
                > is
                > like the http://en.wikipedia.org/wiki/Ron_Popeil Showtime Rotisseries of
                > Lingo,
                > "Set it and forget it!"
                >
                > A timeout object runs independently of your other lingo code; when you set
                > the
                > timeout object and it's parameters it immediately begins the timing and
                > calls
                > the handler you specified every xxx milliseconds based off the period you
                > specified. There's really no other magic to it than that.
                >



                • 5. Re: Problem setting up Timer
                  Chunick Level 3
                  Just trying to cater to my audience. I sacrificed a more complete explanation for one that makes more sense and is a better foundation for someone not as familiar with Director as you or I might be, alchemist. Anyways, thanks for fleshing out the details, nonetheless.