俄罗斯方块之心(cocos2d-js+html5)

xin.jpg

对于俄罗斯方块相信小伙伴们都不陌生,下面来介绍一下最近练手的一款小作品。

先看一下运行效果图(因为简书上传视频不太会。。。):


image.png
image.png

就截取了两张图,有兴趣的朋友可以去微博上查阅视频版。
微博地址:
http://video.weibo.com/show?fid=1034:4267117143380625

好了下面介绍一下代码。

首先介绍一下项目结构:


相信做过web开发的都不陌生这个结构。

index.html 此次要运行动画的主网页
index.js 动画运行的代码
cocos2d-js-v3.13.js 代码API文件

index.html 文件:
主要作用是启动画布,并且调用两个js文件。

<!DOCTYPE html>
<html>
<head>
    <title>心</title>
    <script type="text/javascript" src="cocos2d-js-v3.13.js" charset="UTF-8"></script>
</head>
<body bgcolor="#00000">
    <canvas id="gameCanvas" width="1500" height="720"></canvas>
    <script type="text/javascript" src="index.js" charset="UTF-8"></script>
</body>
</html>

cocos2d-js-v3.13.js文件是官网上下载的便不再多说,主要说明一下index.js文件。

index.js文件:

var line = 26;      //每个方格的边长
var gap = 4;        //每个方格之间的距离
var xin = [];       //
var srcX = 0;
var srcY = 650;
var xinDx = 1500 - (1500/2)/2 - 199;
var xinDy = (720/2)/2 - 100;
                    //  1   2   3   4   5   6   7   8   9   10  11  12  13  
var xinMode = new Array(0 , 2 , 0 , 0 , 0 , 0 , 0 , 2 , 1 , 2 , 1 , 0 , 0 , 
                        // 14  15  16  17  18  19  20  21  22  23  24  25  26
                            0 , 2 , 2 , 2 , 2 , 0 , 1 , 0 , 2 , 0 , 2 , 1 , 2 );
var xinType = new Array(1 , 1 , 3 , 3 , 1 , 3 , 2 , 0 , 2 , 1 , 3 , 0 , 0 , 
                            3 , 0 , 2 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 1 , 1 );
                            
var xinP = new Array(cc.p(xinDx                 , xinDy + 4*line + 3*gap),//1
                    cc.p(xinDx + line*2 + gap*2 , xinDy + 4*line + 3*gap),//2
                    cc.p(xinDx - line - gap     , xinDy + 3*line + 2*gap),//3
                    cc.p(xinDx - 3*line - 3*gap , xinDy + 5*line + 4*gap),//4
                    cc.p(xinDx - 2*line - 2*gap , xinDy + 5*line + 4*gap),//5
                    cc.p(xinDx                  , xinDy + 6*line + 5*gap),//6
                    cc.p(xinDx + 3*line + 3*gap , xinDy + 5*line + 4*gap),//7
                    cc.p(xinDx + 3*line + 3*gap , xinDy + 6*line + 5*gap),//8
                    cc.p(xinDx + 6*line + 6*gap , xinDy + 7*line + 6*gap),//9
                    cc.p(xinDx + line + gap     , xinDy + 6*line + 5*gap),//10
                    cc.p(xinDx - 5*line - 5*gap , xinDy + 6*line + 5*gap),//11
                    cc.p(xinDx - 4*line - 4*gap , xinDy + 7*line + 6*gap),//12
                    cc.p(xinDx + 4*line + 4*gap , xinDy + 9*line + 8*gap),//13          
                    cc.p(xinDx - 4*line - 4*gap , xinDy + 9*line + 8*gap),//14
                    cc.p(xinDx - 3*line - 3*gap , xinDy + 8*line + 7*gap),//15
                    cc.p(xinDx                  , xinDy + 9*line + 8*gap),//16
                    cc.p(xinDx + 3*line + 3*gap , xinDy + 9*line + 8*gap),//17
                    cc.p(xinDx + line + gap     , xinDy + 9*line + 8*gap),//18
                    cc.p(xinDx - 6*line - 6*gap , xinDy + 10*line + 9*gap),//19
                    cc.p(xinDx + 5*line + 5*gap , xinDy + 11*line + 10*gap),//20
                    cc.p(xinDx                  , xinDy + 10*line + 9*gap),//21
                    cc.p(xinDx - 3*line - 3*gap , xinDy + 10*line + 9*gap),//22
                    cc.p(xinDx + line*2 + gap*2 , xinDy + 11*line + 10*gap),//23
                    cc.p(xinDx - 3*line - 3*gap , xinDy + 12*line + 11*gap),//24
                    cc.p(xinDx + 4*line + 4*gap , xinDy + 12*line + 11*gap),//25
                    cc.p(xinDx - 4*line - 4*gap , xinDy + 12*line + 11*gap),//26
                    );;

var step = 0;

function sleep(d){
  for(var t = Date.now();Date.now() - t <= d;);
}

//设置颜色列表
var colList = new Array(cc.color(138,43,226,255),
                    cc.color(255,246,143,255),
                    cc.color(0 ,255,255,255),
                    cc.color(255 ,106 ,106,255),
                    cc.color(255 ,0 ,255,255),
                    cc.color(0 ,0 ,238,255),
                    cc.color(238 ,130 ,98,255),
                    cc.color(255 ,20 ,147,255),
                    cc.color(0 ,238 ,118,255),
                    cc.color(255 ,131 ,250,255),
                    cc.color(155 ,48 ,255,255),
                    cc.color(0 ,229 ,238,255),
                    cc.color(232 ,232 ,232,255),
                    cc.color(144, 238 ,144,255)
                    );

//心的颜色
var redCpl = new Array(cc.color(255,0,0,255),
                        cc.color(255,128,0,255 ),
                        cc.color(255,255,0,255),
                        cc.color(0,255,0,255),
                        cc.color(0,255,255,255),
                        cc.color(0,0,255,255),
                        cc.color(128,0,255,255),);
//心的颜色的索引
var redIndex = 0;
//获取随机数
function GetRandomNum()
{    
    var Rand = Math.random();   
    return Math.round(Rand * 14);   
}  

//方块类
var squ = function(x,y){
    this.a = cc.p(x,y);
    this.b = cc.p(x+line,y);
    this.c = cc.p(x,y-line);
    this.d = cc.p(x+line,y-line);
    
    //方块的四个坐标点
    this.setP = function(x,y){
        this.a = cc.p(x,y);
        this.b = cc.p(x+line,y);
        this.c = cc.p(x,y-line);
        this.d = cc.p(x+line,y-line);
    };
}

//长方块类
var stick = function(x,y){
    this.one = new squ(x,y);                    //第一个方块
    this.two = new squ(this.one.b.x + gap, y);  //第二个方块
    this.thr = new squ(this.two.b.x + gap, y);  //第三个方块
    this.for = new squ(this.thr.b.x + gap, y);  //第四个方块
    this.col = colList[GetRandomNum()];         //随机获取颜色
    this.draw = new cc.DrawNode();              //获取绘图对象
    this.gaibian = 0;                           //目前变化的形态值
    this.bianhuaMax = 2;                        //可以变化的形态最大值    
    
    var bianNum = 0;
    var goNUM = 0;
        
    var gaibianTmp = 0;
    var msecTmp = 0;
    var nextBian = 0;
    
    //绘制图形
    this.drawTuxing = function(){
        //fill
        this.draw.drawRect(this.one.a, this.one.d, this.col, 1, this.col);
        this.draw.drawRect(this.two.a, this.two.d, this.col, 1, this.col);
        this.draw.drawRect(this.thr.a, this.thr.d, this.col, 1, this.col);
        this.draw.drawRect(this.for.a, this.for.d, this.col, 1, this.col);
    }
    
    //设置图形的变化模式
    this.setMode = function(type){
    
    }
    
    //改变图形的形态
    this.bianhuan = function(step){
        //this.draw.clear();
        if(this.gaibian != step){
            this.gaibian = (this.gaibian + 1)%2;
            if(this.gaibian == 0){
                this.two.setP(this.one.b.x + gap, this.one.a.y);
                this.thr.setP(this.two.b.x + gap, this.one.a.y);
                this.for.setP(this.thr.b.x + gap, this.one.a.y);
            
            }else{
                this.two.setP(this.one.a.x, this.one.c.y - gap);
                this.thr.setP(this.one.a.x, this.two.c.y - gap);
                this.for.setP(this.one.a.x, this.thr.c.y - gap);
            
            }
        }
        //this.drawTuxing();
    }
    
    //图形移动实现
    this.act = function(x,y,step){
        bianNum = step + this.bianhuaMax + 1;
        goNUM = x/10;
    
        
        msecTmp++;
        if(msecTmp >= goNUM / bianNum){
            gaibianTmp++;
            msecTmp = 0;
        }
        
        this.draw.clear();
        if(this.one.a.x <= x)
            this.setXY(10,0);
    
        if(this.one.a.x >= x && this.one.a.y >= y)
            this.setXY(0,-10);
        
        if(gaibianTmp == 1){
            if(this.gaibian == 0)
                nextBian++;
                
            if(nextBian == 1){
                this.bianhuan(10);
            }
            else{
                this.bianhuan(step);
            }
            
            gaibianTmp=0;
        }
            
        this.drawTuxing();
    }
    
    //设置图形的坐标
    this.setXY = function(x,y){
        this.two.setP(this.two.a.x + x, this.two.a.y + y);
        this.thr.setP(this.thr.a.x + x, this.thr.a.y + y);
        this.for.setP(this.for.a.x + x, this.for.a.y + y);
        this.one.setP(this.one.a.x + x, this.one.a.y + y);
    }
    
    //设置图形的颜色
    this.setColor = function(){
        this.col = colList[GetRandomNum()];
        this.drawTuxing();
    }
    
    //图形组成心后,设置心的颜色
    this.setRedColor = function(){
        this.col = redCpl[redIndex];
        this.drawTuxing();
    }
}

//T方块类
var tzi = function(x,y){
    this.one = new squ(x,y);
    this.two = new squ(this.one.c.x , this.one.c.y - gap);
    this.thr = new squ(this.one.b.x + gap, this.one.b.y);
    this.for = new squ(this.thr.b.x + gap, this.thr.b.y);
    this.col = colList[GetRandomNum()];
    this.draw = new cc.DrawNode();
    this.gaibian = 0;
    this.type = 1;
    this.bianhuaMax = 4;
    
    var bianNum = 0;
    var goNUM = 0;
        
    var gaibianTmp = 0;
    var msecTmp = 0;
    var nextBian = 0;
    
    
    this.drawTuxing = function(){
        //fill
        this.draw.drawRect(this.one.a, this.one.d, this.col, 1, this.col);
        this.draw.drawRect(this.two.a, this.two.d, this.col, 1, this.col);
        this.draw.drawRect(this.thr.a, this.thr.d, this.col, 1, this.col);
        this.draw.drawRect(this.for.a, this.for.d, this.col, 1, this.col);
    }
    
    this.setMode = function(type){
        if(type == 1){
            this.type = 1;
        }else{
            this.two.setP(this.one.a.x , this.one.a.y + gap + line);
            this.type = 2;
        }
    }
    
    this.bianhuan = function(step){
        //this.draw.clear();
        if(this.gaibian != step){
            this.gaibian = (this.gaibian + 1)%4;
            //fill
            switch(this.gaibian){
                case 0:
                    if(this.type == 1){
                        this.two.setP(this.one.c.x , this.one.c.y - gap);
                    }else{
                        this.two.setP(this.one.a.x , this.one.a.y + gap + line);
                    }
                    this.thr.setP(this.one.b.x + gap, this.one.b.y);
                    this.for.setP(this.thr.b.x + gap, this.thr.b.y);
                break;
                case 1: 
                    if(this.type == 1){
                        this.two.setP(this.one.a.x - gap - line, this.one.a.y);
                    }else{
                        this.two.setP(this.one.a.x + gap + line, this.one.a.y);
                    }
                    
                    this.thr.setP(this.one.a.x, this.one.a.y - line - gap);
                    this.for.setP(this.thr.a.x, this.thr.a.y - line - gap);
                break;
                case 2:
                    if(this.type == 1){
                        this.two.setP(this.one.a.x , this.one.a.y + gap + line);
                    }else{
                        this.two.setP(this.one.a.x , this.one.a.y - gap - line);
                    }
                    
                    this.thr.setP(this.one.a.x - gap - line, this.one.a.y);
                    this.for.setP(this.thr.a.x - gap - line, this.thr.a.y);
                break;
                case 3:
                    if(this.type == 1){
                        this.two.setP(this.one.a.x + gap + line, this.one.a.y);
                    }else{
                        this.two.setP(this.one.a.x - gap - line, this.one.a.y);
                    }
                    
                    this.thr.setP(this.one.a.x, this.one.a.y + line + gap);
                    this.for.setP(this.thr.a.x, this.thr.a.y + line + gap);
                break;
            
            }
        }
        //this.drawTuxing();
    }
    
    this.act = function(x,y,step){
        bianNum = step + this.bianhuaMax + 1;
        goNUM = x/10;
    
        
        msecTmp++;
        if(msecTmp >= goNUM / bianNum){
            gaibianTmp++;
            msecTmp = 0;
        }
        
        this.draw.clear();
        if(this.one.a.x <= x)
            this.setXY(10,0);
    
        if(this.one.a.x >= x && this.one.a.y >= y)
            this.setXY(0,-10);
        
        if(gaibianTmp == 1){
            if(this.gaibian == 0)
                nextBian++;
                
            if(nextBian == 1){
                this.bianhuan(10);
            }
            else{
                this.bianhuan(step);
            }
            
            gaibianTmp=0;
        }
            
        this.drawTuxing();
    }
    
    
    this.setXY = function(x,y){
        this.two.setP(this.two.a.x + x, this.two.a.y + y);
        this.thr.setP(this.thr.a.x + x, this.thr.a.y + y);
        this.for.setP(this.for.a.x + x, this.for.a.y + y);
        this.one.setP(this.one.a.x + x, this.one.a.y + y);
    }
    
    this.setColor = function(){
        this.col = colList[GetRandomNum()];
        this.drawTuxing();
    }
    
    this.setRedColor = function(){
        this.col = redCpl[redIndex];
        this.drawTuxing();
    }
}

//Z方块类
var zzi = function(x,y){
    this.one = new squ(x,y);
    this.two = new squ(this.one.b.x + gap, y);
    this.thr = new squ(this.two.a.x, this.two.a.y + line + gap);
    this.for = new squ(this.thr.b.x + gap, this.thr.b.y);
    this.col = colList[GetRandomNum()];
    this.draw = new cc.DrawNode();
    this.gaibian = 0;
    this.type = 1;
    this.bianhuaMax = 2;
    
    var bianNum = 0;
    var goNUM = 0;
        
    var gaibianTmp = 0;
    var msecTmp = 0;
    var nextBian = 0;
    
    this.setMode = function(type){
        if(type == 1){
            this.type = 1;
        }else{
            this.one.setP(this.two.a.x, this.two.a.y);
            this.two.setP(this.thr.a.x, this.thr.a.y);
            this.thr.setP(this.one.a.x, this.one.a.y);
            this.one.setP(this.two.a.x - gap - line, this.two.a.y);
            this.for.setP(this.thr.b.x + gap, this.thr.b.y);
            this.type = 2;
        }
    }
    
    this.drawTuxing = function(){
        //fill
        this.draw.drawRect(this.one.a, this.one.d, this.col, 1, this.col);
        this.draw.drawRect(this.two.a, this.two.d, this.col, 1, this.col);
        this.draw.drawRect(this.thr.a, this.thr.d, this.col, 1, this.col);
        this.draw.drawRect(this.for.a, this.for.d, this.col, 1, this.col);
    }
    
    this.bianhuan = function(step){
        //this.draw.clear();
        if(this.gaibian != step){
            this.gaibian = (this.gaibian + 1)%2;
            if(this.type == 1){
                if(this.gaibian == 0){
                    this.for.setP(this.thr.a.x, this.thr.a.y);
                    this.thr.setP(this.two.a.x, this.two.a.y);
                    this.two.setP(this.thr.c.x, this.thr.c.y - gap);
                    this.one.setP(this.two.a.x - gap - line, this.two.a.y);
                    
                }else{
                    this.one.setP(this.thr.a.x, this.thr.a.y + gap + line);
                    this.two.setP(this.thr.a.x, this.thr.a.y);
                    this.thr.setP(this.for.a.x, this.for.a.y);
                    this.for.setP(this.thr.a.x, this.thr.c.y - gap);
                    
                    
                }
            }else{
                if(this.gaibian == 0){
                    this.one.setP(this.thr.a.x - gap - line,this.thr.a.y);
                    this.two.setP(this.thr.a.x, this.thr.a.y);
                    this.thr.setP(this.for.a.x, this.for.a.y);
                    this.for.setP(this.thr.b.x + gap, this.thr.a.y);
                }else{
                    this.for.setP(this.thr.a.x, this.thr.a.y);
                    this.thr.setP(this.two.a.x, this.two.a.y);
                    this.two.setP(this.thr.b.x + gap,this.thr.b.y);
                    this.one.setP(this.two.a.x , this.two.a.y + gap + line);
                }
            }
        }
        //this.drawTuxing();
    }
    
    this.act = function(x,y,step){
        bianNum = step + this.bianhuaMax + 1;
        goNUM = x/10;
    
        
        msecTmp++;
        if(msecTmp >= goNUM / bianNum){
            gaibianTmp++;
            msecTmp = 0;
        }
        
        this.draw.clear();
        if(this.one.a.x <= x)
            this.setXY(10,0);
    
        if(this.one.a.x >= x && this.one.a.y >= y)
            this.setXY(0,-10);
        
        if(gaibianTmp == 1){
            if(this.gaibian == 0)
                nextBian++;
                
            if(nextBian == 1){
                this.bianhuan(10);
            }
            else{
                this.bianhuan(step);
            }
            
            gaibianTmp=0;
        }
            
        this.drawTuxing();
    }
    
    
    this.setXY = function(x,y){
        this.two.setP(this.two.a.x + x, this.two.a.y + y);
        this.thr.setP(this.thr.a.x + x, this.thr.a.y + y);
        this.for.setP(this.for.a.x + x, this.for.a.y + y);
        this.one.setP(this.one.a.x + x, this.one.a.y + y);
    }
    
    this.setColor = function(){
        this.col = colList[GetRandomNum()];
        this.drawTuxing();
    }
    
    this.setRedColor = function(){
        this.col = redCpl[redIndex];
        this.drawTuxing();
    }
}

//土方块类
var tuzi = function(x,y){
    this.two = new squ(x,y);
    this.one = new squ(this.two.b.x + gap, y);
    this.thr = new squ(this.one.a.x, this.one.a.y + line + gap);
    this.for = new squ(this.thr.b.x + gap, y);
    this.fiv = new squ(this.one.c.x, this.one.c.y - gap);
    
    this.col = colList[GetRandomNum()];
    this.draw = new cc.DrawNode();
    this.gaibian = 0;
    this.bianhuaMax = 4;
    
    var bianNum = 0;
    var goNUM = 0;
        
    var gaibianTmp = 0;
    var msecTmp = 0;
    var nextBian = 0;
    
    
    this.drawTuxing = function(){
        //fill
        this.draw.drawRect(this.one.a, this.one.d, this.col, 1, this.col);
        switch(this.gaibian){
            case 0:
                this.draw.drawRect(this.two.a, this.two.d, this.col, 1, this.col);
                this.draw.drawRect(this.thr.a, this.thr.d, this.col, 1, this.col);
                this.draw.drawRect(this.for.a, this.for.d, this.col, 1, this.col);
            break;
            case 1: 
                this.draw.drawRect(this.fiv.a, this.fiv.d, this.col, 1, this.col);
                this.draw.drawRect(this.thr.a, this.thr.d, this.col, 1, this.col);
                this.draw.drawRect(this.for.a, this.for.d, this.col, 1, this.col);
            break;
            case 2:
                this.draw.drawRect(this.two.a, this.two.d, this.col, 1, this.col);
                this.draw.drawRect(this.fiv.a, this.fiv.d, this.col, 1, this.col);
                this.draw.drawRect(this.for.a, this.for.d, this.col, 1, this.col);
            break;
            case 3:
                this.draw.drawRect(this.two.a, this.two.d, this.col, 1, this.col);
                this.draw.drawRect(this.thr.a, this.thr.d, this.col, 1, this.col);
                this.draw.drawRect(this.fiv.a, this.fiv.d, this.col, 1, this.col);
            break;
        }
    }
    
     this.setMode = function(type){
    
    }
    
    this.bianhuan = function(step){
        //this.draw.clear();
        
        if(this.gaibian != step)
            this.gaibian = (this.gaibian + 1)%4;
        
        //this.drawTuxing();
    }
    
    this.act = function(x,y,step){
        bianNum = step + this.bianhuaMax + 1;
        goNUM = x/10;
    
        
        msecTmp++;
        if(msecTmp >= goNUM / bianNum){
            gaibianTmp++;
            msecTmp = 0;
        }
        
        this.draw.clear();
        if(this.one.a.x <= x)
            this.setXY(10,0);
    
        if(this.one.a.x >= x && this.one.a.y >= y)
            this.setXY(0,-10);
        
        if(gaibianTmp == 1){
            if(this.gaibian == 0)
                nextBian++;
                
            if(nextBian == 1){
                this.bianhuan(10);
            }
            else{
                this.bianhuan(step);
            }
            
            gaibianTmp=0;
        }
            
        this.drawTuxing();
    }
    
    
    this.setXY = function(x,y){
        this.two.setP(this.two.a.x + x, this.two.a.y + y);
        this.thr.setP(this.thr.a.x + x, this.thr.a.y + y);
        this.for.setP(this.for.a.x + x, this.for.a.y + y);
        this.one.setP(this.one.a.x + x, this.one.a.y + y);
        this.fiv.setP(this.fiv.a.x + x, this.fiv.a.y + y);
    }
    
    this.setColor = function(){
        this.col = colList[GetRandomNum()];
        this.drawTuxing();
    }
    
    this.setRedColor = function(){
        this.col = redCpl[redIndex];
        this.drawTuxing();
    }
}

//田方块类
var tianzi = function(x,y){
    this.one = new squ(x,y);
    this.two = new squ(this.one.b.x + gap, this.one.b.y);
    this.thr = new squ(this.one.c.x, this.one.c.y - gap);
    this.for = new squ(this.thr.b.x + gap, this.thr.b.y);
    this.col = colList[GetRandomNum()];
    this.draw = new cc.DrawNode();
    this.gaibian = 0;
    this.bianhuaMax = 0;
    var bianNum = 0;
    var goNUM = 0;
        
    var gaibianTmp = 0;
    var msecTmp = 0;
    var nextBian = 0;
    
    this.drawTuxing = function(){
        //fill
        this.draw.drawRect(this.one.a, this.one.d, this.col, 1, this.col);
        this.draw.drawRect(this.two.a, this.two.d, this.col, 1, this.col);
        this.draw.drawRect(this.thr.a, this.thr.d, this.col, 1, this.col);
        this.draw.drawRect(this.for.a, this.for.d, this.col, 1, this.col);
    }
    
     this.setMode = function(type){
    
    }
    
    this.act = function(x,y,step){
        bianNum = step + this.bianhuaMax + 1;
        goNUM = x/10;
    
        
        msecTmp++;
        if(msecTmp >= goNUM / bianNum){
            gaibianTmp++;
            msecTmp = 0;
        }
        
        this.draw.clear();
        if(this.one.a.x <= x)
            this.setXY(10,0);
    
        if(this.one.a.x >= x && this.one.a.y >= y)
            this.setXY(0,-10);
        
        if(gaibianTmp == 1){
            if(this.gaibian == 0)
                nextBian++;
                
            if(nextBian == 1){
                this.bianhuan(10);
            }
            else{
                this.bianhuan(step);
            }
            
            gaibianTmp=0;
        }
            
        this.drawTuxing();
    }
    
    
    this.bianhuan = function(step){
    
    }
    
    this.setXY = function(x,y){
        this.two.setP(this.two.a.x + x, this.two.a.y + y);
        this.thr.setP(this.thr.a.x + x, this.thr.a.y + y);
        this.for.setP(this.for.a.x + x, this.for.a.y + y);
        this.one.setP(this.one.a.x + x, this.one.a.y + y);
    }
    
    this.setColor = function(){
        this.col = colList[GetRandomNum()];
        this.drawTuxing();
    }
    
    this.setRedColor = function(){
        this.col = redCpl[redIndex];
        this.drawTuxing();
    }
}

//cocos2d引擎动作开始
window.onload = function(){
    var msecTmp = 0;
    var secTmp = 0;
    
    cc.game.onStart = function(){
        //构建场景
        var MyScene = cc.Scene.extend({
        
          ctor:function () {
            this._super();
            this.schedule(this.updateOneSec,0.05);
          },
          onEnter:function () {
            this._super();
            
            var winSize = cc.director.getWinSize();
            //构建层,并加入精灵
            xin.push(new stick(srcX,srcY));
            xin.push(new zzi(srcX,srcY));
            xin.push(new tuzi(srcX,srcY));
            xin.push(new tuzi(srcX,srcY));
            xin.push(new tuzi(srcX,srcY));
            xin.push(new tuzi(srcX,srcY));
            xin.push(new tuzi(srcX,srcY));
            xin.push(new tzi(srcX,srcY));
            xin.push(new tzi(srcX,srcY));
            xin.push(new tzi(srcX,srcY));
            xin.push(new tzi(srcX,srcY));
            xin.push(new stick(srcX,srcY));
            xin.push(new tianzi(srcX,srcY));
            xin.push(new tuzi(srcX,srcY));
            xin.push(new tzi(srcX,srcY));
            xin.push(new tzi(srcX,srcY));
            xin.push(new zzi(srcX,srcY));
            xin.push(new tzi(srcX,srcY));
            xin.push(new stick(srcX,srcY));
            xin.push(new zzi(srcX,srcY));
            xin.push(new stick(srcX,srcY));
            xin.push(new tzi(srcX,srcY));
            xin.push(new tuzi(srcX,srcY));
            xin.push(new zzi(srcX,srcY));
            xin.push(new tzi(srcX,srcY));
            xin.push(new zzi(srcX,srcY));
            
            for(var i = 0; i < 26; i++){
                this.addChild(xin[i].draw, 10);
            }
            
            xin[step].setMode(xinMode[step]);
            
          },
          
          //时间回调,实现动画
          updateOneSec:function () {
    
            if(step < 26){
                if(xin[step].one.a.x >= xinP[step].x && xin[step].one.a.y <= xinP[step].y){
            
                    step++;
                    if(step < 26){
                        xin[step].setMode(xinMode[step]);
                    }
                }
                if(step < 26){
                    xin[step].act(xinP[step].x, xinP[step].y, xinType[step]);
                }
            }
            else{
                msecTmp++;
                if(msecTmp >= 10){
                    if(secTmp <=  5){
                        for(var j = 0; j < step; j++){
                            xin[j].setColor();
                        }
                        secTmp++;
                    }else{
                        for(var j = 0; j < step; j++){
                            xin[j].setRedColor();
                        }
                        
                        redIndex=(redIndex+1)%7;
                    }
                    msecTmp=0;
                }
            }
            
          }
        });
        //绘制动画
        cc.director.runScene(new MyScene());
    };
    cc.game.run("gameCanvas");
};

主要的解释都写在注释里。因为每个俄罗斯方块模型的类都很相似,所以只重点介绍了长方形的模型的类。

OK,打完收工。

源码gitHub位置:
https://github.com/BYManbu/heart.git

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,142评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,298评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,068评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,081评论 1 291
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,099评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,071评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,990评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,832评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,274评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,488评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,649评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,378评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,979评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,625评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,796评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,643评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,545评论 2 352

推荐阅读更多精彩内容