How can I split cc.Class to definition across multiple files

Hi all,

Since my script is getting bigger, I want to split cc.Class script to multiple files.
I did research for how to do it, and find this,


So, I wrote code below, but it isn’t working. Can anybody tell me how to fix it?
It seems require(‘CB_X.js’)(CharBase); part is not working.
Thanks in advance.

CharaBase.js

 require('CB_A.js')(CharBase);
 require('CB_ABjs')(CharBase);
var CharBase = cc.Class({
    onLoad: function() {
        CharBase.methodA();
        this.methodB();
    },
});
module.exports = CharBase;

CB_A.js

module.exports = function(CharBase) { 
    CharBase.prototype.methodA = function() {
        cc.log('Class method A');
    };
    // more methods...
};

CB_B.js

module.exports = function(CharBase) { 
    CharBase.methodB = function() {
        cc.log('Instance method B');
    };
    // more methods...
};

put requires AFTER you define CharBase in CharBase.js :slight_smile:

1 Like

You helping me several times. Big thanks to you!

Actually, the solution is below.
Should define instance method wit ‘prototype’, class method without it.

CharaBase.js

var CharBase = cc.Class({
    onLoad: function() {
        CharBase.methodA();
        this.methodB();
    },
});
require('CB_A.js')(CharBase);
require('CB_B.js')(CharBase);
module.exports = CharBase;

CB_A.js

module.exports = function(CharBase) { 
    CharBase.methodA = function() {
        cc.log('Class method A');
    };
    // more methods...
};

CB_B.js

module.exports = function(CharBase) { 
    CharBase.prototype.methodB = function() {
        cc.log('Instance method B');
    };
    // more methods...
};