cocosCreator跨类调用

//游戏管理器
var TGameManager = cc.Class({
    extends: cc.Component,

    properties: {

    },
    //单例
    statics:{
        self:null,

        getInstance:function()
        {
            if (TGameManager.self == null) {

                var node=new cc.Node('TGameManager');

                TGameManager.self = node.addComponent(TGameManager);
             };

            return TGameManager.self;
        },
    },

    //构造函数
    ctor(){
        TGameManager.self = this;

        this.scene = new Array();
    },

    onLoad () {
        cc.game.addPersistRootNode(this.node);

        this.initGameConfige();

        this.initResouces();   
    },

    //切换场景 
    start () {
        //setTimeout() 是属于 window 的方法,该方法用于在指定的毫秒数后调用函数
  //      setTimeout(function(){//延时
//
  //      cc.director.loadScene(this.runScene);
//
      //  }.bind(this),3000);//.bin(this)绑定自己       
    },
    
    registerScene:function(sceneScript){

        this.scenes.push(sceneScript);

    },
    unregisterScene:function(idex)
    {
        this.scene[idx]=null;
    },

    getSceneScript:function(idx){
        return this.scenes[idx];
    },

    //初始化游戏设置
    initGameConfige:function(){

    },
    //初始化资源
    initResouces:function(){

    },

    //进入游戏场景
    startGame:function()
    {
        cc.director.loadScene('TGameScene');
    },

    //退出游戏 
    exitGame:function(){
        cc.game.end();
    },

   update (dt) {},
});
//导出类
module.exports = TGameManager;

 

var TGameManager = require('TGameManager');//导入类

cc.Class({
    extends: cc.Component,

    properties: {

    },

    onLoad () {
        this.gm = TGameManager.getInstance();

        cc.log("gm="+this.gm);
    },

    start () {
    },
//按钮回调
    clickButton:function(event,customEventData){
        if (customEventData == 1) {
            this.gm.startGame();//this.gm.startGame();
        };

        else if (customEventData == 4) {
            this.gm.exitGame();
        };
    },
    update (dt) { 
    
    },
});

cocosCreator跨类调用_第1张图片

 

你可能感兴趣的:(cocoCreator_JS)