cocos creater 实现消除星星小游戏

 

var global_point = require("globalpoints");
cc.Class({
    extends: cc.Component,

    properties: {
        starList: [cc.Prefab],//不同星星列表
        stars: [cc.Prefab],//星星数组
        clear_stars: [cc.Prefab],//消除星星的队列
        box: {
            default: null,
            type: cc.Prefab,
        },
        boxList: [cc.Prefab],//盒子
        isschedule: false,

    },
    onLoad: function () {
        global_point.point = 0;
        global_point.points = 0;
        var windowsize = cc.view.getDesignResolutionSize();
        var windowx = windowsize.width / 3;
        var windowy = windowsize.height / 4;
        for (var i = 0; i < 8; i++) {//初始化星星
            for (var j = 0; j < 8; j++) {
                var star_number = Math.round(Math.random() * 4 + 1);
                this.add_star(star_number, j * 50 + windowx, i * 50 + windowy, i * 8 + j + 1);
            }
        }
       // this.isschedule = true;
      //  this.schedule(this.init_stars, 0.2);//每0.5秒添加星星

    },
    unschedule_this: function () {
      //  this.isschedule = false;
    },
    add_star: function (number, x, y, tags) {
        var star = cc.instantiate(this.starList[number - 1]);//实例化星星
        star.setPosition(cc.v2(x, y));
        star.addComponent("starmgr");//添加脚本
        star.tag = tags;
        this.node.addChild(star);
        this.stars.push(star);//8*8  
    },
    check_border: function (star)//判断是否可以删除该位置的星星
    {
        var self = this;
        var sprite;
        sprite = star.getComponent(cc.Sprite);
        if (cc.isValid(sprite)) {
            self.clear_stars.push(star);//遍历过的星星
        }
        if ((star.tag) % 8 != 0) {//右边界判断
            if (star.name == self.stars[star.tag].name) {
                var is_clear1 = false;
                var is_run1 = 0;
                for (var i = 0; i < self.clear_stars.length; i++) {
                    if (self.clear_stars[i].tag == self.stars[star.tag].tag) {
                        is_clear1 = true;
                    }
                    is_run1++;
                }
                if (!is_clear1 && is_run1 == self.clear_stars.length) {
                    self.check_border(self.stars[star.tag]);
                }
            }
        }
        if ((star.tag) % 8 != 1) {//左边界
            if (star.name == self.stars[star.tag - 2].name) {
                var is_clear2 = false;
                var is_run2 = 0;

                for (var i = 0; i < self.clear_stars.length; i++) {
                    if (self.clear_stars[i].tag == self.stars[star.tag - 2].tag) {
                        is_clear2 = true;
                    }
                    is_run2++;
                }
                if (!is_clear2 && is_run2 == self.clear_stars.length) {
                    self.check_border(self.stars[star.tag - 2]);
                }
            }
        }
        if ((star.tag - 9) >= 0) {//下边界
            if (star.name == self.stars[star.tag - 9].name) {
                var is_clear3 = false;
                var is_run3 = 0;

                for (var i = 0; i < self.clear_stars.length; i++) {
                    if (self.clear_stars[i].tag == self.stars[star.tag - 9].tag) {
                        is_clear3 = true;
                    }
                    is_run3++;
                }
                if (!is_clear3 && is_run3 == self.clear_stars.length) {
                    self.check_border(self.stars[star.tag - 9]);
                }
            }
        }
        if ((star.tag + 7) < self.stars.length) {//上边界

            if (star.name == self.stars[star.tag + 7].name) {

                var is_clear4 = false;
                var is_run4 = 0;
                for (var i = 0; i < self.clear_stars.length; i++) {
                    if (self.clear_stars[i].tag == self.stars[star.tag + 7].tag) {
                        is_clear4 = true;
                    }
                    is_run4++;
                };
                if (!is_clear4 && is_run4 == self.clear_stars.length) {
                    self.check_border(self.stars[star.tag + 7]);

                }

            }
        }
        else {
  
        }
    },
    init_clear_star: function () {//初始化消除星星的数组
        this.clear_stars.splice(0, this.clear_stars.length);
    },
    add_box: function () {//添加盒子
        var self = this;
        if (self.clear_stars.length < 3) { return; }
        self.clear_stars.forEach(function (star) {
            var box = cc.instantiate(self.box);
            var x = star.getPositionX();
            var y = star.getPositionY();
            box.setPosition(cc.v2(x, y));
            self.node.addChild(box);
            self.boxList.push(box);
        });
    },
    clear_box: function () {//销毁盒子
        var self = this;
        var index = 0;
        var prise = cc.find("Canvas/praise");
        var jspraise = prise.getComponent("praise");
        global_point.points += self.boxList.length;//加分
        if (self.boxList.length > 4 && self.boxList.length < 6) {

            jspraise.add_efect(1, 0, 0);
        };
        if (self.boxList.length >= 6) {

            jspraise.add_efect(2, 0, 0);
        };

        self.boxList.forEach(function (box) {
            var sprite = box.getComponent(cc.Sprite);
            sprite.node.active = false;
            sprite.destroy();
            self.node.removeChild(box);

        });
        self.boxList.splice(0, self.boxList.length);
        global_point.point = 0;

    },
    clear_star: function ()//消除3个以上的星星
    {

        var self = this;
        if (self.clear_stars.length >= 3) {

            self.clear_stars.forEach(function (star) {//
                var sprite = star.getComponent(cc.Sprite);
                sprite.node.active = false;
                sprite.destroy();
            });
            self.clear_stars.splice(0, this.clear_stars.length);
        }
    },
    fill_star: function () {//下落星星
        var index = 0;
        var self = this;
            self.stars.forEach(function (star) {//删除销毁的星星
                var sprite = star.getComponent(cc.Sprite);//的星星的精灵
                if (star.tag > 8 && cc.isValid(star))//存在下边界
                {
                    var sprite = self.stars[star.tag - 9].getComponent(cc.Sprite);//下行的星星的精灵
                    if (!cc.isValid(sprite)) {//如果删除了精灵替换位置,tag,下标
                        var pos = self.stars[index - 8].getPosition();
                        var pos1 = star.getPosition();
                        var temp = self.stars[index - 8];
                        self.stars.splice(index - 8, 1, star);//换数组位置
                        self.stars.splice(index, 1, temp);//换数组位置
                        star.tag = star.tag - 8;//换tag值
                        self.stars[index].tag = star.tag + 8;//换tag值
                        star.setPosition(pos);
                        self.stars[index].setPosition(pos1);
                    }
                }
                index++;
            });
    },
    init_stars: function () {//替换删除的星星
            var index = 0;
            var self = this;
            self.stars.forEach(function (star) {//删除销毁的星星
                var sprite = star.getComponent(cc.Sprite);//下行的星星的精灵
                if (!cc.isValid(sprite)) {
                    var number = Math.round(Math.random() * 4 + 1);
                    var x = star.getPositionX();
                    var y = star.getPositionY();
                    var tags = star.tag;
                    var newstar = cc.instantiate(self.starList[number - 1]);//实例化星星
                    newstar.setPosition(cc.v2(x, y));
                    newstar.addComponent("starmgr");//添加脚本
                    newstar.tag = tags;
                    self.node.addChild(newstar);
                    self.stars.splice(index, 1, newstar);
                }
                index++;
            });
        
    },
    // called every frame, uncomment this function to activate update callback
    update: function (dt) {
        this.fill_star();//下落星星
        this.init_stars();
    },

});

 

  1. 星星类
    var global_point = require("globalpoints");
    cc.Class({
        extends: cc.Component,
    
        properties: {
            position_index: 0,
            is_add_box: false,
        },
        onLoad: function () {
            var self = this;
            var index = 0;
            self.node.on(cc.Node.EventType.TOUCH_START, function () {//global_starlist.starList
                if (!self.is_add_box) {
                    self.add_box_first();
                }
                index++;
                if (self.is_add_box && index == 2) {
                    self.check_star();
                    index = 0;
                }
            });
    
        },
        destroy_star: function () {
            var sprite = this.getComponent(cc.Sprite);
            sprite.node.active = false;
            sprite.destroy();
        },
        check_star: function () {
            var index = 0;
            var x = this.node.getPositionX();
            var y = this.node.getPositionY();
            var gird = cc.find("Canvas/gird");
            var jsgird = gird.getComponent("girdarithmetic");
            jsgird.clear_star();//满足后消除
            this.is_add_box = false;
            jsgird.clear_box();
        },
        add_box_first: function () {
            var gird = cc.find("Canvas/gird");
            var jsgird = gird.getComponent("girdarithmetic");
            jsgird.init_clear_star();//初始化消除星星队列
            jsgird.check_border(this.node);//检测点击星星是否满足消除条件
            jsgird.add_box();
            this.is_add_box = true;
    
        },
        clear_box: function () {
            var gird = cc.find("Canvas/gird");
            var jsgird = gird.getComponent("girdarithmetic");
            jsgird.init_clear_star();//初始化消除星星队列
            jsgird.check_border(this.node);//检测点击星星是否满足消除条件
            jsgird.clear_box();
            this.is_add_box = false;
    
        },
    
        // called every frame, uncomment this function to activate update callback
        update: function (dt) {
    
        },
    });
    

     

你可能感兴趣的:(cocos creater 实现消除星星小游戏)