1 Reply Latest reply on Sep 25, 2006 4:16 PM by Rothrock

    pounding head against keyboard....please help

    calmchess333 Level 1
      well once agian i've corrupted my code and I don't have a clue how i did it. I have 3 objects on stage.....msg.text.....count.text.....view_btn.

      If i execute the below code using onLoad every thing works beautiffuly B
      ut if i execute it useing ....._root.view_btn.onPress = test; ..................it displays the text before the timer has reached 0.......................(if statement on line 48)


      What have I done wrong? Where do i find the proper way to write this stuff...........none of it is mentioned in any of the three flash books which i own............please help me.

        • 1. Re: pounding head against keyboard....please help
          Rothrock Level 5
          Take a page out of Sherlock Holmes – or just the logic of if it is happening it must be for a reason.

          So if the "test2" message is appearing then milli MUST be less or equal to zero. So if milli is getting to zero before the timer is showing 0:00 you must figure out why that is. My guess is that you are setting the time for the countdown to be .11 minutes from the time the onLoad happens, but the onPress doesn't happen at the same instant. But I'm not really following all your script, and to me it seems to be a bit all over the place.

          The secret to writing these things correctly is making a plan and doing the things that need doing. Simple, huh? :) For me the trick is to first make an algorithm for how the thing should be done – without regard to Actionscript, just an abstract set of steps. So something like this.

          1. Define how long a countdown I want.

          2. At some point in time start the countdown – take a note of what time this starts.

          3. Every little bit of time check the current time and figure out how long it has been since we started (step 2).

          4. If the elapsed time in step 3 is not long enough, update my counter. If it has been long enough update my counter and my done indicator.

          5. If I'm not done go back to step 3 until I am. If I am done stop doing this.

          This seems to be a good description of the task – regardless of if I am doing this with a stopwatch or if I'm going to try and program it in BASIC or Actionscript. Once I have the basic outline, I start looking for things in Flash that match up.

          Step one seems to be setting a variable.

          Step two seems to be looking for some event. If I want an onLoad event that is fine or if I want on onPress event that is also fine. I think that in your example you were using two different events, but that isn't a good idea – as you've noticed. At this point I would also use getTimer(). It returns the number of milliseconds since this instance of the Flash player started.

          Step three calls for something that does things "every little bit." In AS you have the basic choices of onEnterFrame and setInterval. Personally I would use setInterval, but let's just stick with onEnterFrame for this right now. (I don't think you really need to check this 20 or more times a second.) Also some basic math and more uses of getTimer().

          Step four require some if() and more math.

          Step five is a very important part that many people seem to forget. If you were personally assigned to to a 1 minute countdown, what would you be doing 3 or 4 hours later? Probably not continuing to watch the stopwatch and do the math, right? But with Flash if you don't stop an onEnterFrame or clear an interval they will continue to keep going – even if their purpose is long past. So be sure to add a part that deletes the onEnterFrame or clears the interval!

          TADA!

          For many common tasks I don't have to make these kinds of outlines anymore. But if I've got something new or complicated or if I'm having a problem I do it.