Module.exports question?

Hi,

I have the following:

scoremanager.js
onLoad:function(){
this.scorezombie=50;
this.PadZombieScore();

 //  this.ZombieHitIncreaseScore();
},</code>

module.exports.name = this.scorezombie;

gameoverscore.js
var finalscore = require(‘scoremanager’);
cc.Class({
extends: cc.Component,

properties: {
   **zombiefinalscore:**{
        default:null,
       type:cc.Label
        
    }
},

// use this for initialization
onLoad: function () {
this.finalzombiescore=0;
this.GetZombieScore();
  },

GetZombieScore:function(){
this.output = finalscore.name;
cc.log(‘final score:’+finalscore.name);
if(this.finalzombiescore<=9999) {this.finalzombiescore = (“00000”+this.finalzombiescore).slice(-6)}
this.finalzombiescore = this.output;
this.zombiefinalscore.string = this.finalzombiescore.toString();

}

What I’m trying to display is the score of 50, however, it’s giving me undefined. How can I pass the score to the gameoverscene. I understand that I can have the gameoverscene within the mainscene which is where my main game is, however, I would like to learn by doing it through module.exports / require methods…Any help will be greatly appreciated it…Thanks for your time and God Bless…

Sincerely,

Sunday

The finalscore variable is undefined because you’re trying to require the scoremanager function that you haven’t exported. If you want to use require(), you could export scoremanager as a variable and use scorezombie as a static variable.
To do this, modify:

// scoremanager.js
var scorezombie = 50;
var scoremanager = cc.Class({
    extends: cc.Component,
    properties: {
    ...
    },
    statics: {
        scorezombie: scorezombie
    },
    ...
});
module.exports = scoremanager;

// gameoverscore.js
var scoremanager = require('scoremanager');
cc.Class({
    ...
    GetZombieScore: function () {
        ...
        this.zombiefinalscore.string = scoremanager.scorezombie;
    }
});

But if you don’t want to use a static variable and want to use a member variable of a scoremanager object, you could get a reference to a scoremanager component you’ve instantiated by drag-and-dropping the Node containing it in the scene editor or by calling cc.find(), then calling getComponent() on the Node.

1 Like

Hi @chakim

Thanks for the help…really appreciated it…:slight_smile: God Bless…

Sincerely,

Sunday