CocosCreator learning modular script

CocosCreator learning modular script

Cocos Creator modular script

Cocos 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:

  • Access parameters exported from other files
  • Call other file export methods
  • Use other file export types
  • Use or inherit other Component

JavaScript in Cocos Creator uses CommonJS standards that are almost the same as Node.js to achieve modularity. In short:

  • Each separate script file constitutes a module
  • Each module is a separate scope
  • Use the synchronous require method to reference other modules
  • Set module.exports to the exported variable

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 :

// 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 module.exports is:
When your module.exports has no definition, Creator will automatically set exports to the Component defined in the script. If the script does not define a Component but defines another type of CCClass, exports will be automatically set to the defined CCClass.

Exporting variables

module.exports is an empty object ( {} ) by default, and you can add new fields directly to it.

// 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 module.exports can be any JavaScript type.

// 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:
  • Unity3D realizes camera lens movement and limits the angle
  • Detailed explanation of how to use several timers in CocosCreator
  • How to use physics engine joints in CocosCreator
  • How to use JSZip compression in CocosCreator
  • CocosCreator Getting Started Tutorial: Making Your First Game with TS
  • Interpretation of CocosCreator source code: engine startup and main loop
  • CocosCreator general framework design resource management
  • How to make a List in CocosCreator
  • How to use http and WebSocket in CocosCreator
  • Analysis of CocosCreator's new resource management system
  • How to use cc.follow for camera tracking in CocosCreator

<<:  Tutorial on installing lamp-php7.0 in Centos7.4 environment

>>:  Detailed analysis of SQL execution steps

Recommend

Detailed explanation of how to enable slow query log in MySQL database

The database enables slow query logs Modify the c...

HTML form tag tutorial (1):

Forms are a major external form for implementing ...

Javascript Virtual DOM Detailed Explanation

Table of contents What is virtual dom? Why do we ...

How to use vite to build vue3 application

1. Installation Tip: There is currently no offici...

A brief discussion on the problem of Docker run container being in created state

In a recent problem, there is such a phenomenon: ...

Example of how to build a Harbor public repository with Docker

The previous blog post talked about the Registry ...

Detailed explanation of Linux host name modification command

Linux change hostname command 1. If you only need...

6 Uncommon HTML Tags

First: <abbr> or <acronym> These two s...

Practical tutorial on modifying MySQL character set

Preface: In MySQL, the system supports many chara...

MySQL 5.7.12 installation and configuration tutorial under Mac OS 10.11

How to install and configure MySQL on Mac OS 10.1...

Implementing a simple age calculator based on HTML+JS

Table of contents Preface Demonstration effect HT...

Analysis of Mysql transaction characteristics and level principles

1. What is a transaction? A database transaction ...

Detailed explanation of CocosCreator message distribution mechanism

Overview This article begins to introduce content...