·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]as3.0基于系统时间的时间管理器Ticker类(2)

时间:2015-09-13 22:18酷播
这里没有实现,是因为我写了两种定时器,一种是基于时间的定时器,一种是基于帧频的定时器。 下面分别是两种定时器的代码: packageorg.easily.ticker { importflash.utils.Dictionary; /** *@authorEasily */ publ

这里没有实现,是因为我写了两种定时器,一种是基于时间的定时器,一种是基于帧频的定时器。

下面分别是两种定时器的代码:

  1. package org.easily.ticker 
  2. { 
  3.     import flash.utils.Dictionary; 
  4.       
  5.     /** 
  6.      * @author Easily 
  7.      */ 
  8.     public class TimeTicker extends Ticker 
  9.     { 
  10.         public var delay:Number; 
  11.         public var repeatCount:int; 
  12.         public var tickTime:Number; 
  13.         public var tickCount:int; 
  14.           
  15.         public function TimeTicker(delay_:Number, repeatCount_:int = 0, timerFunc_:Function = null, compFunc_:Function = null) 
  16.         { 
  17.             super(timerFunc_, compFunc_); 
  18.             delay = Math.abs(delay_); 
  19.             repeatCount = Math.max(0, repeatCount_); 
  20.               
  21.             reset(); 
  22.         } 
  23.           
  24.         override public function reset():void 
  25.         { 
  26.             tickCount = 0; 
  27.             tickTime = 0; 
  28.         } 
  29.   
  30.         override public function doTick(dtime:Number):void 
  31.         { 
  32.             tickTime += dtime; 
  33.             while (tickTime >= delay) 
  34.             { 
  35.                 tickTime -= delay; 
  36.                 ++tickCount; 
  37.                 if (timerFunc != null) 
  38.                 { 
  39.                     timerFunc(); 
  40.                 } 
  41.                 if (repeatCount > 0 && tickCount >= repeatCount) 
  42.                 { 
  43.                     if (compFunc != null) 
  44.                     { 
  45.                         compFunc(); 
  46.                     } 
  47.                     dispose(); 
  48.                     return; 
  49.                 } 
  50.             } 
  51.         } 
  52.     } 
  53. } 

 

  1. package org.easily.ticker 
  2. { 
  3.     import flash.utils.Dictionary; 
  4.       
  5.     /** 
  6.      * @author Easily 
  7.      */ 
  8.     public class FrameTicker extends Ticker 
  9.     { 
  10.         public var totalCount:int; 
  11.         public var tickCount:int; 
  12.         public var frequency:int; 
  13.         public var repeatCount:int; 
  14.           
  15.         public function FrameTicker(frequency_:int = 1, repeatCount_:int = 0, timerFunc_:Function = null, compFunc_:Function = null) 
  16.         { 
  17.             super(timerFunc_, compFunc_); 
  18.             frequency = Math.max(1, frequency_); 
  19.             repeatCount = Math.max(0, repeatCount_); 
  20.               
  21.             reset(); 
  22.         } 
  23.           
  24.         override public function reset():void 
  25.         { 
  26.             tickCount = 0; 
  27.         } 
  28.   
  29.         override public function doTick(dtime:Number):void 
  30.         { 
  31.             ++tickCount; 
  32.               
  33.             if (tickCount == frequency) 
  34.             { 
  35.                 tickCount = 0; 
  36.                 ++totalCount; 
  37.                 if (timerFunc != null) 
  38.                 { 
  39.                     timerFunc(dtime); 
  40.                 } 
  41.                 if (repeatCount > 0 && totalCount >= repeatCount) 
  42.                 { 
  43.                     if (compFunc != null) 
  44.                     { 
  45.                         compFunc(); 
  46.                     } 
  47.                     dispose(); 
  48.                 } 
  49.             } 
  50.         } 
  51.     } 
  52. } 

 有了两种定时器,应该需要一个管理器才方便,于是有了一个 TickerManager:

  1. package org.easily.ticker 
  2. { 
  3.     import org.easily.utils.ArrayUtils; 
  4.       
  5.     /** 
  6.      * @author Easily 
  7.      */ 
  8.     public class TickerManager 
  9.     { 
  10.         public static var sInstance:TickerManager = new TickerManager(); 
  11.           
  12.         public static function getInstance():TickerManager 
  13.         { 
  14.             return sInstance; 
  15.         } 
  16.           
  17.         public var tickerList:Array = []; 
  18.           
  19.         public function TickerManager() 
  20.         { 
  21.             if (sInstance) 
  22.             { 
  23.                 throw new Error("TickerManager Singleton already constructed!"); 
  24.             } 
  25.         } 
  26.           
  27.         public function get length():int 
  28.         { 
  29.             return tickerList.length; 
  30.         } 
  31.           
  32.         public function doTick(dtime:Number):void 
  33.         { 
  34.             for each (var ticker:Ticker in ArrayUtils.clone(tickerList)) 
  35.             { 
  36.                 ticker.doTick(dtime); 
  37.             } 
  38.         } 
  39.           
  40.         public function addTicker(ticker:Ticker):void 
  41.         { 
  42.             ArrayUtils.push(tickerList, ticker); 
  43.         } 
  44.           
  45.         public function removeTicker(ticker:Ticker):void 
  46.         { 
  47.             ArrayUtils.remove(tickerList, ticker); 
  48.         } 
  49.     } 
  50. } 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器