在舞台上绘制progressBar元件,注册点设置为左边。代码如下:
1 package 2 { 3 import flash.display.MovieClip; 4 import flash.events.Event; 5 6 public class ProgressBar extends MovieClip 7 { 8 private var totalTime:Number; 9 private var startTime:Number; 10 private var stopTime:Number; 11 12 public function ProgressBar() 13 { 14 initGame(); 15 } 16 17 /** 18 * 初始化游戏 19 */ 20 private function initGame():void 21 { 22 //设置初始数据 23 setData(); 24 } 25 26 /** 27 * 设置游戏数据 28 */ 29 private function setData():void 30 { 31 startTime = 0; 32 totalTime = 30; 33 progressBar.scaleX = 1; 34 // 设定计时器 35 setGameTimer(); 36 } 37 38 /** 39 * 设置计时器 40 */ 41 private function setGameTimer():void 42 { 43 var startDate:Date = new Date(); 44 startTime = startDate.getTime(); 45 totalTime = totalTime * 1000; 46 this.addEventListener(Event.ENTER_FRAME, onEnterFrame); 47 } 48 49 /** 50 * 刷新游戏进度条 51 */ 52 private function onEnterFrame(e:Event):void 53 { 54 var nowTime:Date = new Date(); 55 var passTime:Number = nowTime.getTime() - startTime; 56 progressBar.scaleX = 1 - passTime / totalTime; 57 if (passTime > totalTime) 58 { 59 endGame(); 60 } 61 } 62 63 /** 64 * 游戏结束 65 */ 66 private function endGame():void 67 { 68 this.removeEventListener(Event.ENTER_FRAME, onEnterFrame); 69 trace("gameOver") 70 } 71 } 72 73 }