13 Replies Latest reply on Oct 13, 2006 9:06 PM by kglad

    problem with getTimer();

    sbryner Level 1
      Hello,

      I've been creating a stopwatch type application that works like a gas pump. the starts button start the price raising like your pumping gas and once it reaches a random number it "Explodes".

      Well, everything works. Problem is when it reaches the desired "explodePrice" var it passes it SOMETIMES
      and SOMETIMES it hits it.

      I use the code below but need something that when it hits the exact number it executes the "if" function...
      I've tried various was of stating the if () statement including: if() else, if() else if() and others

      if (theTime == explodePrice){
      and
      if(theTime>= explodedPrice){

      but nothing stops at the exact random price. it seems sometimes it skips the price..
      any thoughts... new to time and milliSeconds...

      sky
        • 1. Re: problem with getTimer();
          kglad Adobe Community Professional & MVP
          use:

          • 2. Re: problem with getTimer();
            sbryner Level 1
            It still passes my "explodePrice" before realizing
            "theTime==explodePrice"

            below is the code I use. Is there something I'm missing?
            • 3. Re: problem with getTimer();
              I believe it is because you are using Math.floor because when you say it "sometimes" hits the exact number.

              I am assuming that explode time is an integer so you may want to use Math.ceil or you can add 1 to the first "if" conditional statement.
              • 4. Re: problem with getTimer();
                sbryner Level 1
                I tried the if() idea
                if(theTime+1==explodePrice){
                gotoAndPlay("myexplode");
                }


                and the Math.ceil()

                theTime = Math.ceil((getTimer()-startTimer)/10);



                neither work.... i get outputs that skip the explodePrice


                Output:
                the price is 141
                it should explode when it reaches 178
                the price is 154
                it should explode when it reaches 178
                the price is 167
                it should explode when it reaches 178
                the price is 181
                it should explode when it reaches 178

                it only stops if I use

                if(theTime>= explodePrice){
                }
                if I use:

                if(theTime==explodePrice){
                }

                it never stops.

                it seems to skip outputing certain milliSeconds?
                • 5. Re: problem with getTimer();
                  I didn't read the whole post but yes I see now where your problem is. You should not be using the getTimer() function in what you are trying to do because it will not == theExplode time. It will hit it sometimes yes, but not consistently because you are restricted to your fps.

                  What I would suggest is either having a setInterval call or just use a counter with an onEnterFrame event.

                  • 6. Re: problem with getTimer();
                    Here is a quick example of the Date class which you maybe able to apply just put a dynamic text field on the stage in frame 1 and name the instance "time_txt" and add this code to frame 1.


                    • 7. Re: problem with getTimer();
                      kglad Adobe Community Professional & MVP
                      why test for exact equality if you want the explosion to occur when getTimer() equals or excedes your random number?
                      • 8. Re: problem with getTimer();
                        sbryner Level 1
                        I want the explosion to occur when the getTimer()
                        equals the random number... It seems to only work if
                        I use the: getTimer >=explosionPrice {

                        if I use : getTimer==explosionPrice {

                        it just passes the random variable that was generated.

                        I'm trying to change up the info. poisionriver helped me with but it's a little tougher for me than what I have already. What I have works it just passes it. the getTimer doesn't always register the explosionPrice is why it doesn't work everytime.
                        • 9. Re: problem with getTimer();
                          kglad Adobe Community Professional & MVP
                          and that's what you would expect if you're going to text for exact equality.

                          well, that's what i'd expect. what do you you expect to happen when you test getTimer() for a match with a random number?
                          • 10. Re: problem with getTimer();
                            sbryner Level 1
                            well, I figured it worked like this...

                            milliSeconds "or getTimer()" counts up 1,2,3,4.....

                            when it reaches the random variable say, 1023
                            it finishes the function.

                            getTimer()....1021,1022,1023....BOOM!

                            but it seems to skip numbers... so getTimer() doesn't hit every number?
                            at least that's how I thought it worked it counted every milliSecond.
                            though in the output it seems to leap 144, 157, 168,181.

                            Am i getting milliSeconds and getTimer() confused? Is not the getTimer based on the milliSeconds?
                            • 11. Re: problem with getTimer();
                              kglad Adobe Community Professional & MVP
                              getTimer() reports the number of milliseconds that have transpired since your swf began. however, the frequency of its reporting depends upon the frequency that you execute getTimer().

                              ie, if you executed getTimer() every millisecond it would count sequentially as you expected. however, you cannot call it every millisecond because your onEnterFrame method only executes (about) every 1000/fps milliseconds, where fps = your swf's frames per second setting.

                              in addition, the system resources required to execute any code every millisecond are probably not present in your current computer so setting your swf's fps to 1000 (which you can't even try) wouldn't work. likewise, using a setInterval() loop calling a function every millisecond (which you can try) wouldn't work, either

                              now, back to my main point. there's no perceivable difference between getTimer() (that you currently have in your onEnterFrame method) counting each and every ms up to your random number and then triggering your explosion and having getTimer() trigger your explosion when it first excedes your random number.
                              • 12. Re: problem with getTimer();
                                sbryner Level 1
                                thanks kglad, i think I understand it more now.
                                • 13. Re: problem with getTimer();
                                  kglad Adobe Community Professional & MVP
                                  you're welcome.