Cocos2d-js is getting slower and slower timer schedule making constant slow Timer

Source: Internet
Author: User

Cocos2d-js is getting slower and slower timer schedule making constant slow Timer
There may be a slight error in animation control, but you will be aware of the error of schedule if you accumulate multiple cycles or perform network synchronization. First, let's give an example to prove that the copied code var InaccuracyTestLayer = cc. layer. extend ({ctor: function () {this. _ super (); var startTime = new Date (). getTime (); var count = 0; this. schedule (function () {var timePass = new Date (). getTime ()-startTime; count ++; var delta = timePass-(count * 100); trace ("time pass", timePass, "total delta", delta, "count", count);}, 0.1); this. scheduleUpdate () ;}, update: function () {for (v Ar I = 0; I <100000; I ++) {B = 1/0. 22222 ;}}}); the lower the frame rate, the faster the code replication speed. Time pass, 1481, total delta, 381, count, 11 CCDebugger. js: 334 time pass, 1608, total delta, 408, count, 12 CCDebugger. js: 334 time pass, 1735, total delta, 435, count, 13 CCDebugger. js: 334 time pass, 1861, total delta, 461, count, 14 CCDebugger. js: 334. How can I solve the problem? Timer principle: cocos2d-js bottom layer in each frame calculation, traverse all the timer, to see whether to reach the trigger time. If the timer is reached, the timer is triggered and the time is reset to the current time. Well, the problem lies in this: "reset to the current time ". Look at a new Timer: copy the code schedule2: function (callback, interval) {var then = Date. now (); integer = interval * 1000; this. schedule (function () {var now = Date. now (); var delta = now-then; if (delta> interval) {then = now-(delta % interval); callback. call (this );}}. bind (this), 0);} copy the code. The core here is then = now-(delta % interval). The error is calculated to the control of the next trigger each time. For example, if 60fps is triggered every 16 ms for schedule2 and 112 ms interval is set for the user, there will be 16*7 => frames to trigger the user's timer once. The error of 12 ms is accumulated here, and 12 ms is counted into then. So next time there will be 12 + 16*6 = 108> 100, 2nd user timers will be triggered in 96 ms, 4 ms ahead of schedule, making up for 1st errors. This timer can withstand the test and remains stable even when the frame rate is low. Copy the code var BetterScheduleLayer = cc. layer. extend ({ctor: function () {this. _ super (); var startTime = Date. now (); var count = 0; this. schedule2 (function () {var timePass = Date. now ()-startTime; count ++; var delta = timePass-(count * 100); trace ("time pass", timePass, "total delta", delta, "count", count);}, 0.1); this. scheduleUpdate () ;}, schedule2: function (callback, interval) {var then = Date. now (); integer = interval * 1000; this. schedule (function () {var now = Date. now (); var delta = now-then; if (delta> interval) {then = now-(delta % interval); callback. call (this );}}. bind (this), 0) ;}, update: function () {for (var I = 0; I <10000000; I ++) {B = 1/0. 22222 ;}}); copy the code output: time pass, 3447, total delta, 47, count, 34 CCDebugger. js: 334 time pass, 3510, total delta, 10, count, 35 CCDebugger. js: 334 time pass, 3637, total delta, 37, count, 36 CCDebugger. js: 334 time pass, 3701, total delta, 1, count, 37 CCDebugger. js: 334 time pass, 3828, total delta, 28, count, 38 CCDebugger. js: 334 time pass, 3955, total delta, 55, count, 39 CCDebugger. js: 334

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.