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) { 
    
    },
});

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值