Guest User

Untitled

a guest
Feb 14th, 2012
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     //getTime() method returns the number of milliseconds since midnight January 1, 1970, universal time, for the specified Date object.
  16.     //Use this method to represent a specific instant in time when comparing two or more Date objects.
  17.     currentTime= new Date().getTime();
  18.    
  19.     timeChange=currentTime-previousTime;    // How much time has passed since last frame?
  20.     previousTime=currentTime;               // save currentTime value into previousTime variable
  21.  
  22.    
  23.     if(timerStart)
  24.     {
  25.         timeLeft=timeLeft-timeChange;       // update the time remaining
  26.        
  27.         //convert timeLeft value to seconds and minutes
  28.         //Math.floor returns the closest integer that is less than or equal to the specified number or expression.
  29.         var seconds=Math.floor(timeLeft/1000%60);
  30.         var minutes=Math.floor(timeLeft/1000/60);
  31.        
  32.         //display secondes and minutes in separate textboxes
  33.         minutesText.text=minutes.toString();
  34.         secondsText.text=seconds.toString();
  35.        
  36.         //visualize by applying the percentage of timeLeft to properties of an intstance or to frame number of an animation movie clip.
  37.         box.scaleY=timeLeft/countTime;
  38.         var frameNum=Math.round(animation.totalFrames*(1-timeLeft/countTime));
  39.         animation.gotoAndStop(frameNum);
  40.     }
  41.    
  42.     if(timeLeft<=0)
  43.     {
  44.         timerStart=false;                               //stop the timer
  45.         box.scaleY=0;                                   //stop reducing the box size
  46.         animation.gotoAndStop(animation.totalFrames);   //stop the animation play
  47.         minutesText.text='0';
  48.         secondsText.text='0';
  49.        
  50.     }
  51. }
  52.  
  53. function startTimer(e)
  54. {
  55.     timerStart=true;
  56.     if(var state=0)
  57.     {
  58.         timeStart=true;
  59.     }
  60.     else
  61.     {
  62.         timerStart=true;
  63.         animation.gotoAndStop(animation.first);
  64.         secondsText.text='20';
  65.         timeLeft=(20*1000);
  66.         box.scaleY=1;
  67.         var state=0;
  68.     }
  69. }
  70.  
  71. function pauseTimer(e)
  72. {
  73.     timerStart=false;
  74.     var state=0;
  75. }
  76.  
  77. function stopTimer(e)
  78. {
  79.     timerStart=true;
  80.     animation.gotoAndStop(animation.totalFrames);
  81.     secondsText.text='0';
  82.     timeLeft=0;
  83.     var state=1;
  84. }
  85.  
  86. function resetTimer(e)
  87. {
  88.     timerStart=false;
  89.     animation.gotoAndStop(animation.first);
  90.     secondsText.text='20';
  91.     timeLeft=(20*1000);
  92.     box.scaleY=1;
  93.     var state=0;
  94.    
  95. }
  96.  
  97. addEventListener('enterFrame', update);
  98. startBtn.addEventListener('mouseDown', startTimer);
  99. pauseBtn.addEventListener('mouseDown', pauseTimer);
  100. resetBtn.addEventListener('mouseDown', resetTimer);
  101. stopBtn.addEventListener('mouseDown', stopTimer);
Advertisement
Add Comment
Please, Sign In to add comment