BACKGROUND cocos Creator camera using differential movement

Copyright Notice: Copyright: This article is a blogger original article, reproduced please retain the original link https://blog.csdn.net/weixin_43365995/article/details/88709333

The creator of the design background to move generally use update to refresh the coordinates, but because dt value (needle) will cause instability update in the background when moving Caton particularly uncomfortable or frustrated.
Here are two solutions
1. Use of setTimeout js timing function to write a temporary needle to refresh the background moving
2. creator of the camera function
to say that we here at the timing function creator also has built-in timer why we use setTimeout first timer at the js there are several we sit down a little compare
__ talk about setTimeout __

Suppose we give onclick event of a button bindings this method, when we press the button, be sure to perform content block1 and then run to the place of setTimeout, setTimeout will tell the browser that "after a period of 200ms I would want to plug code execution to your queue ", of course, agreed browser (note inserted in the code does not mean immediately execute), after setTimeout code runs, closely followed by block2 code execution starts here began to explain the problem, if after block2 code execution for more than 200ms, then the result will be what? maybe accordance with the understanding you before, will take for granted that 200ms arrives, your process code immediately execute ... the fact is that in the course of block2 execution (implementation of 200ms ) process queue code is inserted into the code, but has to wait for the end of execution click method, process code segment will be performed, from the point of view of code queue process after the click of the code, plus js performed in a single thread, it should not difficult to understand. If on the other case, the time code execution block2 <200ms, setTimeout 200ms after the process in the code into the code team Column, and then executing thread may already be in the idle state (idle), the result is that after 200ms, process code into a queue to immediately execute, let you feel after 200ms, it performed.
Var myVar;
var = the STEP function () {

       if(循环条件){
         myVar = setTimeout(step, 1000/60);
       }else{
         clearTimeout(myVar) 
       } 
    }
   myVar = setTimeout(step, 1000/60);

Look at setInterval __ __

There may be two problems:
1. 2. The time interval may skip interval may <timing of the execution time of the call code
and we assume as above by a click, triggered setInterval process performed to achieve a time interval codes
such as executing the onclick to 300ms, BLOCK1 end code execution, when the execution setInterval 5ms, this is a point in time when the code insertion process 205ms, click the successful conclusion of the code, the code begins execution process timer code (corresponding to FIG. ), but also performs the process code is a relatively long time, more than one insertion point in time the next 405ms, so that the code is inserted into a process queue after the code, to continue the process, and in excess of 605ms that the insertion point in time, the following questions, you may still think the code will continue to queue behind the insertion of a process of code ... the truth is, process the code because the code already has a queue is not performed, so 605ms the insertion point in time will be "merciless" skip, because js engine allows only a process of code is not executed, said this You do not know it is not it will become clear ...

Summary setTimeout setInterval better accuracy than the high point, we have time to run about two-second time stamp method compare
the topic
is particularly smooth camera movement background Creator, but because it is so moved background rendering coordinate movement actually did not move, so do collision detection of a coordinate system to convert or not to use mobile camera for rendering.
Under the code down to see where the link is particularly simple and
not much to say we just put the code
download link git https://gitee.com/wangxiaoheng/CameraMachine
not git students we can download it directly below the address
https: // download.csdn.net/download/weixin_43365995/11044951

Guess you like

Origin blog.csdn.net/weixin_43365995/article/details/88709333