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
Version Chain In InnoDB engine tables, there are ...
Preface According to the scope of locking, locks ...
1. Multiple calls to single arrow Once a single a...
Usage scenarios For existing servers A and B, if ...
The advantage of the master-slave synchronization...
Detailed explanation of tinyMCE usage initializat...
Common usage of Regexp in Mysql Fuzzy matching, c...
Table of contents Skeleton screen use Vue archite...
Sometimes we build a file server through nginx, w...
I am planning to organize the company's inter...
1. Cause: The effect after the subbox is set to f...
1. Introduction to gitlab Gitlab official address...
Recently, I started upgrading my blog. In the proc...
Preface It's a cliché. Here I will talk about...
1. First introduce several commonly used MySQL fu...