【问题标题】:How to send final game score between Cocos Creator scenes?Cocos Creator 场景之间如何发送最终游戏分数?
【发布时间】:2020-08-21 23:54:27
【问题描述】:

我正在尝试使用 Cocos Creator 创建游戏。我在游戏中使用了多个文件。比如Game.jsGameOver.jsJump.js等。我正在用GainScore.js收集分数。我必须将最终分数发送到GameOver.js 文件。我在比赛中正确显示比分。但是当游戏结束时,我必须将其发送到另一个屏幕。如何将游戏分数用作全局?

我的 gainScore 函数:

  gainScore() {
    this.score += 1;
    if (this.scoreDisplay === null) return;
    this.scoreDisplay.string = this.score;
  },

我的GameOver.js 文件

cc.Class({
    extends: cc.Component,

    properties: {
        scoreEnd: {
            default: null,
            type: cc.Label,
        },
    },

    start() {
        this.scoreEnd.string = this.score.toString(); // I can't access with this way
    },
});

【问题讨论】:

    标签: cocos2d-js


    【解决方案1】:

    您可以使用CommonJS。创建一个名为 Global.js 的新文件。

    scripts
      |__ GameOver.js
      |__ GainScore.js
      |__ Global.js
    

    并在此处保留您的全局变量。

    Global.js:

    module.exports = {
        score: 0
    };
    

    并在其他文件中与 require 一起使用:

    let Globals = require("Globals");
    
    ....
    
    gainScore() {
       Globals.score += 1; // not this.score, should be Globals.score
       if (this.scoreDisplay === null) return;
       this.scoreDisplay.string = Globals.score;
    },
    

    您应该要求所有其他将使用的文件

    let Globals = require("Globals");
    
    cc.Class({
        extends: cc.Component,
    
        properties: {
            scoreEnd: {
                default: null,
                type: cc.Label,
            },
        },
    
        start() {
            this.scoreEnd.string = Globals.score.toString();
        },
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多