宜昌本地软件开发与弱电工程服务商
项目咨询 · 快速响应
您当前的位置: > 博客2 > AS2与AS3技术

非常有用的倒计时效果,倒计时类,游戏倒计时常用

2016-10-13 00:16:48 点击量:11 标签: 收藏本文

这是一个非常有用的倒计时类,常用于游戏当中,用于检测时间的倒计时

倒计时类:

  1. import mx.events.EventDispatcher;     
  2. import mx.utils.Delegate;     
  3. //倒计时     
  4. class game.time.Time {     
  5.   private var _id:Number;     
  6.   private var _second:Number;     
  7.   private var _total:Number;     
  8.   private var dispatchEvent:Function;     
  9.   public var addEventListener:Function;     
  10.   public var removeEventListener:Function;     
  11.   public function Time(s) {     
  12.     EventDispatcher.initialize(this);     
  13.     _second = s;     
  14.     _total = s;     
  15.   }     
  16.   public function getTime() {     
  17.     return _second;     
  18.   }     
  19.   private function timeing() {     
  20.     if (_second>0) {     
  21.       _second--;     
  22.       this.dispatchEvent({type:"onPlay", value:_second});     
  23.     } else {     
  24.       this.dispatchEvent({type:"onStop", value:_second});     
  25.       trace("stop")     
  26.       this.stop();     
  27.     }     
  28.   }     
  29.   public function play() {     
  30.     _id = setInterval(Delegate.create(this, timeing), 1000);     
  31.   }     
  32.   public function stop() {     
  33.     _second = _total;     
  34.     clearInterval(_id);     
  35.   }     
  36. }    

使用代码:

  1. import game.time.Time;     
  2. //倒计时的时间     
  3. var _totalTime = 30;     
  4. var _time = new Time(_totalTime);     
  5. _time.addEventListener("onPlay", timeStart);     
  6. _time.addEventListener("onStop", timeOver);     
  7. //计时中..     
  8. function timeStart(obj) {     
  9.   time = obj.value;     
  10.   score = _score;     
  11.   update();     
  12. }     
  13. //超时,结束游戏     
  14. function timeOver() {     
  15.   gotoAndStop("over");     
  16. }     
  17. startBtn.onRelease=function(){     
  18. //启动倒计时     
  19. _time.play()     
  20. }  

 

ABOUT US
宜昌农人网络科技有限公司

我们是一家提供综合软件外包与弱电工程服务的技术公司。

业务涵盖定制软件、管理系统、网站、小程序、APP、系统集成、网络布线、监控安防、门禁、机房建设及长期技术维护。

公司地址:湖北省宜昌市伍家岗区东站二路6号三峡(宜昌)大数据产业园海洋馆二楼A203室