After timer. Stop, timer. currentcount is not reset. After timer. Reset, currentcount is reset.
Package game. mananger {import flash. events. timerevent; import flash. utils. dictionary; import flash. utils. timer;/*** provides a timer that runs at intervals of 1 second, and can register a callback for several seconds. * This is used to improve performance, there is only one global timer * @ author administrator **/public class gtimermanager extends basemanager {private VaR _ Timer: timer; private VaR _ funary: array; Public Function gtimermanager () {If (! _ Timer) {_ timer = new timer (1000); _ timer. addeventlistener (timerevent. timer, _ interval); _ timer. start (); _ funary = [] ;}} private function _ interval (EVT: timerevent): void {var Len: Int = _ funary. length; For (var I: Int = 0; I <Len; I ++) {var info: gfuninfo = _ funary [I]; If (info. isreset) {info. temp ++; If (info. temp % info. delay = 0) {If (info. fun! = NULL) {info. Fun () ;}} else if (_ timer. currentcount % info. Delay = 0) {If (info. Fun! = NULL) {info. fun () ;}}}/*** register the function * @ Param fun * @ Param senconds several seconds interval */Public Function add (struct: gfuninfo ): void {If (_ funary. indexof (struct) =-1) {_ funary. push (struct); If (struct. isreset) {struct. temp = 0 ;}}/*** remove function * @ Param fun **/Public Function remove (struct: gfuninfo): void {var index: Int = _ funary. indexof (struct); If (index! =-1) {_ funary. splice (struct, 1) ;}}/*** destroy **/Public Function destory (): void {_ funary. length = 0; _ timer. stop (); _ timer = NULL ;}}}
Package game. mananger {public class gfuninfo {/***** @ Param fun callback function * @ Param delay interval seconds * @ isreset re-add to determine whether to continue the previous count */Public Function gfuninfo (fun: function, delay: int, isreset: Boolean) {This. fun = fun; this. delay = delay; this. isreset = isreset;} public var fun: function; Public var delay: int; Public var isreset: Boolean; Public var temp: int ;}}
testGtimer(); private function testGtimer():void { var g:GTimerManager = new GTimerManager(); g.add(new GFunInfo(testGfun,5,true)); g.add(new GFunInfo(testGfun2,1,false)); } private function testGfun():void { trace("testGfun"); } private function testGfun2():void { trace("testGfun2"); }
Flash timer performance optimization, interval every several seconds