Timers.as 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // *****-------------------------------------------
  2. // Author: Mariam Dholkawala
  3. // Copyright 2010
  4. // Countdown Timer for Games using Flash Lite 2.0
  5. // *****-------------------------------------------
  6. class Timers{
  7. private var tInterval:Number;//instance of timer
  8. private var duration:Number;// total ticker time in seconds
  9. private var countDown:Boolean;//should timer tick downwards?
  10. private var countStart:Number;//the actual countdown
  11. private var textName:TextField;//textfield name in the .fla file
  12. //Constructor reads the arguments and stores it in the class properties
  13. public function Timers(pDuration:Number, pCountDown:Boolean, pTextName:TextField){
  14. duration = pDuration;
  15. countDown = pCountDown;
  16. textName = pTextName;
  17. if(countDown){
  18. countStart = duration;
  19. }else{
  20. countStart = 0;
  21. }
  22. }
  23. //Starts the Timer
  24. public function startTimer(){
  25. tInterval = setInterval(this, 'playTimer', 1000);
  26. textName.text = String(countStart);
  27. }
  28. //This method is called everytime a second passes
  29. private function playTimer(){
  30. if(countDown){
  31. countStart--;
  32. if(countStart <= 0){
  33. stopTimer();
  34. }
  35. }else{
  36. countStart++;
  37. if(countStart >= duration){
  38. stopTimer();
  39. }
  40. }
  41. textName.text = String(countStart);
  42. }
  43. //Stops the timer
  44. private function stopTimer(){
  45. clearInterval(tInterval);
  46. }
  47. }