Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This section where we create our varaibles and assign them default values is typically called the 'initialization'
- var currentTime; //what time is it now?
- var timeChange; //save changes in time every frame after the timer starts
- var previousTime=currentTime; //save previous time every frame
- var countTime=20*1000; //1 minute= seconds * milliseconds per second
- var timeLeft=countTime; //our timer is at 100%
- var timerStart=false; //another variable to control the timer on/off
- var state=0;
- animation.stop(); //animation stays stopped until the timer starts
- function update(e)
- {
- //getTime() method returns the number of milliseconds since midnight January 1, 1970, universal time, for the specified Date object.
- //Use this method to represent a specific instant in time when comparing two or more Date objects.
- currentTime= new Date().getTime();
- timeChange=currentTime-previousTime; // How much time has passed since last frame?
- previousTime=currentTime; // save currentTime value into previousTime variable
- if(timerStart)
- {
- timeLeft=timeLeft-timeChange; // update the time remaining
- //convert timeLeft value to seconds and minutes
- //Math.floor returns the closest integer that is less than or equal to the specified number or expression.
- var seconds=Math.floor(timeLeft/1000%60);
- var minutes=Math.floor(timeLeft/1000/60);
- //display secondes and minutes in separate textboxes
- minutesText.text=minutes.toString();
- secondsText.text=seconds.toString();
- //visualize by applying the percentage of timeLeft to properties of an intstance or to frame number of an animation movie clip.
- box.scaleY=timeLeft/countTime;
- var frameNum=Math.round(animation.totalFrames*(1-timeLeft/countTime));
- animation.gotoAndStop(frameNum);
- }
- if(timeLeft<=0)
- {
- timerStart=false; //stop the timer
- box.scaleY=0; //stop reducing the box size
- animation.gotoAndStop(animation.totalFrames); //stop the animation play
- minutesText.text='0';
- secondsText.text='0';
- }
- }
- function startTimer(e)
- {
- timerStart=true;
- if(var state=0)
- {
- timeStart=true;
- }
- else
- {
- timerStart=true;
- animation.gotoAndStop(animation.first);
- secondsText.text='20';
- timeLeft=(20*1000);
- box.scaleY=1;
- var state=0;
- }
- }
- function pauseTimer(e)
- {
- timerStart=false;
- var state=0;
- }
- function stopTimer(e)
- {
- timerStart=true;
- animation.gotoAndStop(animation.totalFrames);
- secondsText.text='0';
- timeLeft=0;
- var state=1;
- }
- function resetTimer(e)
- {
- timerStart=false;
- animation.gotoAndStop(animation.first);
- secondsText.text='20';
- timeLeft=(20*1000);
- box.scaleY=1;
- var state=0;
- }
- addEventListener('enterFrame', update);
- startBtn.addEventListener('mouseDown', startTimer);
- pauseBtn.addEventListener('mouseDown', pauseTimer);
- resetBtn.addEventListener('mouseDown', resetTimer);
- stopBtn.addEventListener('mouseDown', stopTimer);
Advertisement
Add Comment
Please, Sign In to add comment