JS单例模式



// 游戏管理对象,单例类
var GameManager = (function () {

    function _GameManager() {
        // 敌人[波组]
        this.groupVector = [];
        // 敌人[数组]
        this.enemyVector = [];
        // 子弹[数组]
        this.bulletVector = [];
        // 移动点集合[数组]
        this.pointVector = [];
        // 当前[背景]
        this.currBgName = "";
        // 当前[地图]
        this.currMapName = "";
        // 当前[金钱]
        this.money = 0;
        // 当前[关卡]
        this.currLevel = 0;
        // 当前[血量]
        this.currHp = 10;
        // 多少波敌人
        this.groupNum = 0;
        // 当前Level plist文件
        this.currLevelFile = "";
        // 下一Level plist文件
        this.nextLevelFile = "";
        // 是否添加完毕
        this.isAddFinished = false;

        this.clear = function(){
            this.groupVector = [];
            this.enemyVector = [];
            this.bulletVector = [];
            this.pointVector = [];
        };
        // ==============[getter && setter]==============
        this.getGroupVector = function(){
            return this.groupVector;
        };
        this.setGroupVector = function(groupVector){
            this.groupVector = groupVector;
        };

        this.getEnemyVector = function(){
            return this.enemyVector;
        };
        this.setEnemyVector = function(enemyVector){
            this.enemyVector = enemyVector;
        };

        this.getBulletVector = function(){
            return this.bulletVector;
        };
        this.setBulletVector = function(bulletVector){
            this.bulletVector = bulletVector;
        };


        this.getPointVector = function(){
            return this.pointVector;
        };
        this.setPointVector = function(pointVector){
            this.pointVector = pointVector;
        };


        this.getCurrBgName = function(){
            return this.currBgName;
        };
        this.setCurrBgName = function(currBgName){
            this.currBgName = currBgName;
        };

        this.getCurrMapName = function(){
            return this.currMapName;
        };
        this.setCurrMapName = function(currMapName){
            this.currMapName = currMapName;
        };

        this.getMoney = function(){
            return this.money;
        };
        this.setMoney = function(money){
            this.money = money;
        };

        this.getCurrHp = function(){
            return this.currHp;
        };
        this.setCurrHp = function(hp){
            this.currHp = hp;
        };

        this.getGroupNum = function(){
            return this.groupNum;
        };
        this.setGroupNum = function(groupNum){
            this.groupNum = groupNum;
        };

        this.getCurrLevelFile = function(){
            return this.currLevelFile;
        };
        this.setCurrLevelFile = function(currLevelFile){
            this.currLevelFile = currLevelFile;
        };

        this.getNextLevelFile = function(){
            return this.nextLevelFile;
        };
        this.setNextLevelFile = function(nextLevelFile){
            this.nextLevelFile = nextLevelFile;
        };

        this.getIsAddFinished = function(){
            return this.isAddFinished;
        };
        this.setIsAddFinished = function(isAddFinished){
            this.isAddFinished = isAddFinished;
        };
        this.getCurrLevel = function(){
            return this.currLevel;
        };
        this.setCurrLevel = function(level){
            this.currLevel = level;
        };
    }
    //实例容器
    var instance;
    //单例
    var _static = {
        name: 'GameManager',
        getInstance: function () {
            if (instance === undefined) {
                instance = new _GameManager();
            }
            return instance;
        }
    };
    return _static;
})();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值