Flash timer performance optimization, interval every several seconds

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.