SHOW:
|
|
- or go back to the newest paste.
| 1 | // This section where we create our varaibles and assign them default values is typically called the 'initialization' | |
| 2 | ||
| 3 | var currentTime; //what time is it now? | |
| 4 | var timeChange; //save changes in time every frame after the timer starts | |
| 5 | var previousTime=currentTime; //save previous time every frame | |
| 6 | var countTime=20*1000; //1 minute= seconds * milliseconds per second | |
| 7 | var timeLeft=countTime; //our timer is at 100% | |
| 8 | var timerStart=false; //another variable to control the timer on/off | |
| 9 | var state=0; | |
| 10 | ||
| 11 | animation.stop(); //animation stays stopped until the timer starts | |
| 12 | ||
| 13 | function update(e) | |
| 14 | {
| |
| 15 | trace(state); | |
| 16 | //getTime() method returns the number of milliseconds since midnight January 1, 1970, universal time, for the specified Date object. | |
| 17 | //Use this method to represent a specific instant in time when comparing two or more Date objects. | |
| 18 | currentTime= new Date().getTime(); | |
| 19 | ||
| 20 | timeChange=currentTime-previousTime; // How much time has passed since last frame? | |
| 21 | previousTime=currentTime; // save currentTime value into previousTime variable | |
| 22 | ||
| 23 | ||
| 24 | if(timerStart) | |
| 25 | {
| |
| 26 | timeLeft=timeLeft-timeChange; // update the time remaining | |
| 27 | ||
| 28 | //convert timeLeft value to seconds and minutes | |
| 29 | //Math.floor returns the closest integer that is less than or equal to the specified number or expression. | |
| 30 | var seconds=Math.floor(timeLeft/1000%60); | |
| 31 | var minutes=Math.floor(timeLeft/1000/60); | |
| 32 | ||
| 33 | //display secondes and minutes in separate textboxes | |
| 34 | minutesText.text=minutes.toString(); | |
| 35 | secondsText.text=seconds.toString(); | |
| 36 | ||
| 37 | //visualize by applying the percentage of timeLeft to properties of an intstance or to frame number of an animation movie clip. | |
| 38 | box.scaleY=timeLeft/countTime; | |
| 39 | var frameNum=Math.round(animation.totalFrames*(1-timeLeft/countTime)); | |
| 40 | animation.gotoAndStop(frameNum); | |
| 41 | } | |
| 42 | ||
| 43 | if(timeLeft<=0) | |
| 44 | {
| |
| 45 | timerStart=false; //stop the timer | |
| 46 | box.scaleY=0; //stop reducing the box size | |
| 47 | animation.gotoAndStop(animation.totalFrames); //stop the animation play | |
| 48 | minutesText.text='0'; | |
| 49 | secondsText.text='0'; | |
| 50 | ||
| 51 | } | |
| 52 | } | |
| 53 | ||
| 54 | function startTimer(e) | |
| 55 | {
| |
| 56 | - | if(var state=0) |
| 56 | + | if(state=0) |
| 57 | {
| |
| 58 | - | timeStart=true; |
| 58 | + | |
| 59 | state=0; | |
| 60 | } | |
| 61 | else | |
| 62 | {
| |
| 63 | timerStart=true; | |
| 64 | animation.gotoAndStop(animation.first); | |
| 65 | secondsText.text='20'; | |
| 66 | timeLeft=(20*1000); | |
| 67 | - | var state=0; |
| 67 | + | |
| 68 | state=0; | |
| 69 | } | |
| 70 | } | |
| 71 | ||
| 72 | function pauseTimer(e) | |
| 73 | {
| |
| 74 | - | var state=0; |
| 74 | + | |
| 75 | state=0; | |
| 76 | } | |
| 77 | ||
| 78 | function stopTimer(e) | |
| 79 | {
| |
| 80 | timerStart=true; | |
| 81 | animation.gotoAndStop(animation.totalFrames); | |
| 82 | secondsText.text='0'; | |
| 83 | - | var state=1; |
| 83 | + | |
| 84 | state=1; | |
| 85 | } | |
| 86 | ||
| 87 | function resetTimer(e) | |
| 88 | {
| |
| 89 | timerStart=false; | |
| 90 | animation.gotoAndStop(animation.first); | |
| 91 | secondsText.text='20'; | |
| 92 | timeLeft=(20*1000); | |
| 93 | - | var state=0; |
| 93 | + | |
| 94 | state=0; | |
| 95 | ||
| 96 | } | |
| 97 | ||
| 98 | addEventListener('enterFrame', update);
| |
| 99 | startBtn.addEventListener('mouseDown', startTimer);
| |
| 100 | pauseBtn.addEventListener('mouseDown', pauseTimer);
| |
| 101 | resetBtn.addEventListener('mouseDown', resetTimer);
| |
| 102 | stopBtn.addEventListener('mouseDown', stopTimer); |