Cocos Creator modular scriptCocos Creator allows you to split your code into multiple script files and let them call each other. This step is simply called modularization. Modularity allows you to reference other script files in Cocos Creator:
JavaScript in Cocos Creator uses CommonJS standards that are almost the same as Node.js to achieve modularity. In short:
When you declare a component in a script, Creator will export it by default, and other scripts can use this component by directly requiring this module. // Rotate.js cc.Class({ extends: cc.Component, // ... }); SinRotate.js // SinRotate.js var Rotate = require("Rotate"); var SinRotate = cc.Class({ extends: Rotate, update: function (dt) { this.rotation += this.speed * Math.sin(dt); } }); Modules can not only define components, but you can actually export any JavaScript object. Suppose there is a script // config.js - v2 var cfg = { moveSpeed: 10, version: "0.15", showTutorial: true, load: function () { // ... } }; cfg.load(); module.exports = cfg; Now if we want to access the cfg object from another script: // player.js var config = require("config"); cc.log("speed is", config.moveSpeed); The default value of Exporting variables // foobar.js: module.exports.foo = function () { cc.log("foo"); }; module.exports.bar = function () { cc.log("bar"); }; //test.js: var foobar = require("foobar"); foobar.foo(); // "foo" foobar.bar(); // "bar" The value of // foobar.js: module.exports = { FOO: function () { this.type = "foo"; }, bar: "bar" }; //test.js: var foobar = require("foobar"); var foo = new foobar.FOO(); cc.log(foo.type); // "foo" cc.log(foobar.bar); // "bar" The above is the detailed content of modular script learning in CocosCreator. For more information about modular script of CocosCreator, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Tutorial on installing lamp-php7.0 in Centos7.4 environment
>>: Detailed analysis of SQL execution steps
The database enables slow query logs Modify the c...
Forms are a major external form for implementing ...
Table of contents What is virtual dom? Why do we ...
1. Installation Tip: There is currently no offici...
Div basic layout <div class="main"&g...
In a recent problem, there is such a phenomenon: ...
The previous blog post talked about the Registry ...
Linux change hostname command 1. If you only need...
Table of contents background Effect Ideas backgro...
First: <abbr> or <acronym> These two s...
Preface: In MySQL, the system supports many chara...
How to install and configure MySQL on Mac OS 10.1...
Table of contents Preface Demonstration effect HT...
1. What is a transaction? A database transaction ...
Overview This article begins to introduce content...