Cocos Creator development is far from being as easy as it seems at first glance. Some visual development languages, such as VB, are easy to get started, but when you enter the actual combat stage, you may find that problems appear all over the place. But don’t be afraid, the root cause is that you are not familiar with it. There is no doubt about its efficiency, and the official flagship product is exactly it. During the development, I deeply feel that there are still many undetailed or thorough points about the execution order of the Cocos Creator script. Therefore, I will make the following supplements based on the study in the latest period.
One, official documents are the most important
The address is: http://docs.cocos.com/creator/manual/zh/scripting/execution-order.html
but , Which advocates the use of a unified control script to initialize other scripts, there are still some doubts, for example, the official sample code is:
// Game.js
const Player = require('Player');
const Enemy = require('Enemy');
const Menu = require('Menu');
cc.Class({< br /> extends: cc.Component,
properties: {
player: Player,
enemy: Enemy,
menu: Menu
},
< br /> onLoad: function () {
this.player.init();
this.enemy.init();
this.menu.init();
}
});
However, when the three scripts Player.js, Enemy.js and Menu.js are respectively mounted on some UI components, where is the Game.js itself mounted? ? Just simply create an empty node at the same level as the outermost Canvas and mount it on it? Through my experimental analysis, this is a problem.
Second, the execution order of scripts on the same node
The official conclusion is:
“The execution order of component scripts on the same node can be checked by the component in the property inspector. The arrangement order in the control. The components arranged on the top will be executed before the components arranged below. We can adjust the arrangement and execution order of the components through the Move Up and Move Down menus in the gear button in the upper right corner of the component.”< br>There is nothing to doubt about this, and it is easy to understand.
3. Timing of execution of scripts mounted on descendant nodes
If a node is a parent node, there is a child node under it, and each parent and child node has some scripts attached to it , The execution order is to execute the script on the parent node first (there may be more than one), and then execute the script on the child node.
Four. The execution order of the scripts mounted on the sibling nodes
In this case, the script codes mounted on each node are executed in a top-down order.
Fifth, the execution sequence of mounting scripts under normal circumstances
If there is a mounting structure diagram of nodes and scripts as shown below:
Canvas1 (S1.JS script is mounted on itself)
There is a child node BALL under it (S11.JS script is mounted on this node)
Game (S2.JS script is mounted on itself)
There is The child node PLATFORM (this node is equipped with S21.JS scripts)
Therefore, according to the above analysis, there is the following script execution order:
S1.JS S11.JS S2.JS S21 .JS
6. Execution timing of script files not inherited from cc.Component defined by cc.Class
For example, the script file Physics-Settings.js:
< pre>console.info(“I am here…”);
let physicsManager = cc.director.getPhysicsManager();
physicsManager.enabled = true;
physicsManager .debugDrawFlags =
// 0;
// cc.PhysicsManager.DrawBits.e_aabbBit |
cc.PhysicsManager.DrawBits.e_jointBit |
cc.PhysicsManager.DrawBits.e_shapeBit
;
This script is loaded very early, loaded before the development of all software custom scripts, as evidenced by the following picture:
So, as can be seen from the above figure, the order of loading all scripts in the program is:
p>
Cocos2d engine
Plug-in scripts (if there are more than one, load them in alphabetical order of the path in the project)
Ordinary scripts (only one file after packaging, internally initialized in the order of require dependencies) p>
Seven. Summary
It’s easy to get started with Cocos Creator, but it’s not easy to master it in depth. , But with the continuous enrichment of official support, I believe it will further simplify our learning. Regarding the JS script development technology, it is the most important thing in the development of Cocos Creator. With the continuous deepening of this tool, we need to continue to master the JS development technology. This article is only a small experience I have summarized in the recent development of a BOX2D mini game and combined with my own use. I hope you can criticize more for improprieties.