1. [代码][JavaScript]代码     
    
/**
 * @GameName :
 * 2048
 *
 * @DevelopTool:
 * Cocos2d-x Editor (CocosEditor)
 *
 * @time
 * 2014-04-04 pm
 *
 * @Licensed:
 * This showcase is licensed under GPL.
 *
 * @Authors:
 * Programmer: touchSnow
 *
 * @Links:
 * http://www.cocos2d-x.com/ (cocos官方)
 * https://github.com/makeapp      (github)
 * http://blog.csdn.net/touchsnow (csdn博客)
 * http://blog.makeapp.co/ (官方博客)
 * http://www.cocoseditor.com/ (建设中官网)
 *
 * @Contact
 * 邮箱:zuowen@makeapp.co
 * qq群:232361142
 *
 */
 
 
var MainLayer = function () {
    this.initX = 9;
    this.initY = 300;
    this.cellSize = 162;
    this.cellSpace = 18;
    this.totalScore = 0;
    this.scoreLabel = {};
 
    this.back = {};
    this.title = {};
};
 
MainLayer.prototype.onDidLoadFromCCB = function () {
    if (sys.platform == 'browser') {
        this.onEnter();
    }
    else {
        this.rootNode.onEnter = function () {
            this.controller.onEnter();
        };
    }
 
    this.rootNode.onTouchesBegan = function (touches, event) {
        this.controller.onTouchesBegan(touches, event);
        return true;
    };
 
    this.rootNode.onTouchesMoved = function (touches, event) {
        this.controller.onTouchesMoved(touches, event);
        return true;
    };
    this.rootNode.onTouchesEnded = function (touches, event) {
        this.controller.onTouchesEnded(touches, event);
        return true;
    };
 
    this.rootNode.setTouchEnabled(true);
};
 
MainLayer.prototype.getPosition = function (i, j) {
    var px = this.initX + this.cellSize / 2 + i * (this.cellSize + this.cellSpace);
    var py = this.initY + this.cellSize / 2 + j * (this.cellSize + this.cellSpace);
    return cc.p(px, py);
}
 
MainLayer.prototype.onEnter = function () {
    //version
    this.versionNum = indexVersions;
    this.indexVersion = VERSIONS[this.versionNum];
    this.title.setString(this.indexVersion.name + "目标:" + this.indexVersion.array[this.indexVersion.array.length - 1] + "");
 
    var random1 = getRandom(4);
    var random2 = getRandom(4);
    while (random1 == random2) {
        random2 = getRandom(4);
    }
 
    var random11 = getRandom(4);
    var random22 = getRandom(4);
 
    this.tables = new Array(4);
    for (var i = 0; i < 4; i++) {
        var sprites = new Array(4);
        for (var j = 0; j < 4; j++) {
            if (i == random1 && j == random11) {
                sprites[j] = this.newNumber(i, j, 1);
            } else if (i == random2 && j == random22) {
                sprites[j] = this.newNumber(i, j, 1);
            } else {
                sprites[j] = this.newNumber(i, j, 0);
            }
        }
        this.tables[i] = sprites;
    }
 
    this.totalScore = 0;
 
};
 
 
MainLayer.prototype.newNumber = function (i, j, num) {
    var cell = cc.MySprite.create(this.rootNode, "5.png", this.getPosition(i, j), 1);
    var cellLabel = cc.MySprite.createLabel(cell, "");
 
    if (num > 0) {
        cell.setColor(COLOR[num]);
        cellLabel.setVisible(true);
        cellLabel.setString(this.indexVersion.array[num]);
        cellLabel.setFontSize(this.indexVersion.labelFontSize);
    } else {
        cellLabel.setVisible(false);
    }
    cell.data = {col: i, row: j, numberLabel: cellLabel, number: num};
    return cell;
};
 
//direction left
MainLayer.prototype.leftCombineNumber = function () {
    for (var j = 0; j < 4; j++) {
        for (var i = 0; i < 4; i++) {
            var cell = this.tables[i][j];
            if (cell.data.number != 0) {
                var k = i + 1;
                while (k < 4) {
                    var nextCell = this.tables[k][j];
                    if (nextCell.data.number != 0) {
                        if (cell.data.number == nextCell.data.number) {
                            cell.data.number += 1;
                            nextCell.data.number = 0;
                            this.totalScore += SCORES[cell.data.number];
                        }
                        k = 4;
                        break;
                    }
                    k++;
                }
            }
        }
    }
 
    for (j = 0; j < 4; j++) {
        for (i = 0; i < 4; i++) {
            cell = this.tables[i][j];
            if (cell.data.number == 0) {
                k = i + 1;
                while (k < 4) {
                    nextCell = this.tables[k][j];
                    if (nextCell.data.number != 0) {
                        cell.data.number = nextCell.data.number;
                        nextCell.data.number = 0;
                        k = 4;
                    }
                    k++;
                }
            }
        }
    }
 
    this.refreshNumber();
};
 
//direction right
MainLayer.prototype.rightCombineNumber = function () {
    for (var j = 0; j < 4; j++) {
        for (var i = 3; i >= 0; i--) {
            var cell = this.tables[i][j];
            if (cell.data.number != 0) {
                var k = i - 1;
                while (k >= 0) {
                    var nextCell = this.tables[k][j];
                    if (nextCell.data.number != 0) {
                        if (cell.data.number == nextCell.data.number) {
                            cell.data.number += 1;
                            nextCell.data.number = 0;
                            this.totalScore += SCORES[cell.data.number];
                        }
                        k = -1;
                        break;
                    }
                    k--;
                }
            }
        }
    }
 
    for (j = 0; j < 4; j++) {
        for (i = 3; i >= 0; i--) {
            cell = this.tables[i][j];
            if (cell.data.number == 0) {
                k = i - 1;
                while (k >= 0) {
                    nextCell = this.tables[k][j];
                    if (nextCell.data.number != 0) {
                        cell.data.number = nextCell.data.number;
                        nextCell.data.number = 0;
                        k = -1;
                    }
                    k--;
                }
            }
        }
    }
 
    this.refreshNumber();
};
 
MainLayer.prototype.downCombineNumber = function () {
    for (var i = 0; i < 4; i++) {
        for (var j = 0; j < 4; j++) {
            var cell = this.tables[i][j];
            if (cell.data.number != 0) {
                var k = j + 1;
                while (k < 4) {
                    var nextCell = this.tables[i][k];
                    if (nextCell.data.number != 0) {
                        if (cell.data.number == nextCell.data.number) {
                            cell.data.number += 1;
                            nextCell.data.number = 0;
                            this.totalScore += SCORES[cell.data.number];
                        }
                        k = 4;
                        break;
                    }
                    k++;
                }
            }
        }
    }
 
    for (i = 0; i < 4; i++) {
        for (j = 0; j < 4; j++) {
            cell = this.tables[i][j];
            if (cell.data.number == 0) {
                k = j + 1;
                while (k < 4) {
                    nextCell = this.tables[i][k];
                    if (nextCell.data.number != 0) {
                        cell.data.number = nextCell.data.number;
                        nextCell.data.number = 0;
                        k = 4;
                    }
                    k++;
                }
            }
        }
    }
 
    this.refreshNumber();
};
 
//touch up
MainLayer.prototype.upCombineNumber = function () {
    for (var i = 0; i < 4; i++) {
        for (var j = 3; j >= 0; j--) {
            var cell = this.tables[i][j];
            if (cell.data.number != 0) {
                var k = j - 1;
                while (k >= 0) {
                    var nextCell = this.tables[i][k];
                    if (nextCell.data.number != 0) {
                        if (cell.data.number == nextCell.data.number) {
                            cell.data.number += 1;
                            nextCell.data.number = 0;
                            this.totalScore += SCORES[cell.data.number];
                        }
                        k = -1;
                        break;
                    }
                    k--;
                }
            }
        }
    }
 
    for (i = 0; i < 4; i++) {
        for (j = 3; j >= 0; j--) {
            cell = this.tables[i][j];
            if (cell.data.number == 0) {
                k = j - 1;
                while (k >= 0) {
                    nextCell = this.tables[i][k];
                    if (nextCell.data.number != 0) {
                        cell.data.number = nextCell.data.number;
                        nextCell.data.number = 0;
                        k = -1;
                    }
                    k--;
                }
            }
        }
    }
 
    this.refreshNumber();
};
 
MainLayer.prototype.refreshNumber = function () {
    var emptyCellList = [];
    for (var i = 0; i < 4; i++) {
        var numbers = " ";
        for (var j = 0; j < 4; j++) {
            var cell = this.tables[i][j];
            var label = cell.data.numberLabel;
            var cellNumber = cell.data.number;
            if (cellNumber != 0) {
                cell.setColor(COLOR[cellNumber]);
                label.setString(this.indexVersion.array[cellNumber] + " ");
                label.setFontSize(this.indexVersion.labelFontSize);
                label.setVisible(true);http://www.enterdesk.com/special/huangguantp/
                if (cellNumber == (this.indexVersion.array.length - 1)) {
                    //check success皇冠图片
                    var toast = cc.Toast.create(this.rootNode, "成功到达:" + this.indexVersion.array[cellNumber], 2);
                    toast.setColor(cc.c3b(255, 0, 0));
                    this.rootNode.scheduleOnce(function () {
                        cc.BuilderReader.runScene("", "MainLayer");
                    }, 2)
                }
            } else {
                cell.setColor(COLOR[cellNumber]);
                label.setVisible(false);
                emptyCellList.push(cell);
            }
            numbers += "  " + cellNumber;
        }
        cc.log("numbers==" + numbers);
    }
 
 
    //score
    this.scoreLabel.setString("分数:" + this.totalScore);
 
    if (emptyCellList.length < 1) {
        //check fail
        var toast = cc.Toast.create(this.rootNode, "失败!", 2);
        toast.setColor(cc.c3b(255, 0, 0));
        this.rootNode.scheduleOnce(function () {
            cc.BuilderReader.runScene("", "MainLayer");
        }, 2)
    } else {
        //create random cell
        var randomCell = emptyCellList[getRandom(emptyCellList.length)];
        randomCell.data.number = 1;
        randomCell.data.numberLabel.setVisible(true);
        randomCell.data.numberLabel.setString(VERSIONS[this.versionNum].array[1] + "");
        randomCell.data.numberLabel.setFontSize(this.indexVersion.labelFontSize);
        randomCell.setColor(COLOR[1]);
        randomCell.runAction(cc.Sequence.create(cc.ScaleTo.create(0, 0.8), cc.ScaleTo.create(0.5, 1)));
 
    }
 
};
 
MainLayer.prototype.onRefreshClicked = function () {
    cc.BuilderReader.runScene("", "MainLayer");
};
 
MainLayer.prototype.onTouchesBegan = function (touches, event) {
    this.pBegan = touches[0].getLocation();
 
    //back
    var backRect = cc.rectCreate(this.back.getPosition(), [50, 30]);
    if (cc.rectContainsPoint(backRect, this.pBegan)) {
        this.back.runAction(cc.Sequence.create(cc.ScaleTo.create(0.2, 1.1),
            cc.CallFunc.create(function () {
                cc.AudioEngine.getInstance().stopAllEffects();
                cc.BuilderReader.runScene("", "LevelLayer");
            })
        ));
    }
};
 
MainLayer.prototype.onTouchesMoved = function (touches, event) {
};
 
MainLayer.prototype.onTouchesEnded = function (touches, event) {
    this.pEnded = touches[0].getLocation();
    if (this.pBegan) {
        if (this.pEnded.x - this.pBegan.x > 50) {
            this.rightCombineNumber();
        }
 
        else if (this.pEnded.x - this.pBegan.x < -50) {
            this.leftCombineNumber();
        }
 
        else if (this.pEnded.y - this.pBegan.y > 50) {
            this.upCombineNumber();
        }
 
        else if (this.pEnded.y - this.pBegan.y < -50) {
            this.downCombineNumber();
        }
    }
};

2048聚合版开源代码,cocos2d-js编写,基于CocosEditor开发工具,可运行Android,ios,html5等的更多相关文章

  1. 【Node.js】2.开发Node.js选择哪个IDE 开发工具呢

    安装完Node.js之后,就要为它选择一个有利的IDE用于开发. 相比较了多个IDE之后,定位在webstrom和sublime上. 有一个简单的比较: webstorm功能很丰富,前端开发工具的集大 ...

  2. 我使用过的Linux命令之swig - 把C/C++的代码嵌入Java等语言的开发工具

    用途说明 SWIG是Simplified Wrapper and Interface Generator的缩写,其官方站点是http://www.swig.org/.SWIG是个帮助使用C或者C++编 ...

  3. 前端 go.js 流程图基于vue开发项目案例

    一.流程图效果 最近一段时间在研究go.js,它是一款前端开发画流程图的一个插件,也是一个难点,要说为什么是难点,首先,它是依赖画布canvas知识开发.其次,要依赖于内部API开发需求,开发项目需求 ...

  4. CWMP开源代码研究6——libcwmp动态库开发

    原创作品,转载请注明出处,严禁非法转载.如有错误,请留言! email:40879506@qq.com 为了使程序具有通用性,便于扩展和维护.采用了"模块"插入的思想.将设备业务相 ...

  5. Android 开源控件与常用开发框架开发工具类

    Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...

  6. 用HTML5/CSS3/JS开发Android/IOS应用框架大全

    现在人人都想成为安卓/IOS应用开发工程师.其实,安卓/IOS应用可以用很多种语言来实现.由于我们前端开发工程师,对HTML5/CSS/JavaScript的网络编程已经相当熟悉了.所以,今天大家将会 ...

  7. 求推荐go语言开发工具及go语言应该以哪种目录结构组织代码?

    go语言的开发工具推荐? go语言开发普通程序及开发web程序的时候,应该以哪种目录结构组织代码? 求推荐go语言开发工具及go语言应该以哪种目录结构组织代码? >> golang这个答案 ...

  8. 火星坐标、百度坐标、WGS84坐标转换代码(JS、python版)

    火星坐标.百度坐标.WGS84坐标转换代码(JS.python版) 一.JS版本源码 github:https://github.com/wandergis/coordTransform /** * ...

  9. 前端、HTML+CSS+JS编写规范(终极版)

    HTMLCSS文档规范 HTML和CSS文档必须采用UTF-8编码格式: HTML文档必须使用HTML5的标准文档格式: HTMLCSS编写规范 HTML和CSS的标签.属性.类名.ID都必须使用小写 ...

随机推荐

  1. mpvue添加对scss的支持

    转载于:https://blog.csdn.net/butterfly5211314/article/details/80163628 新版的已经支持了,用npm官方即可下载,也就是 npm i sa ...

  2. php excel文件导出之二 图像导出

    PHP文件导出 之图像 和 文字同一时候导出 事实上之前写了个php文件导出.跟这个极为相似,由于项目须要对图像进行导出.查询一番.又写了一个, 这个能实现图像的导出(仅仅能是本地图像,不能使用远程图 ...

  3. Session对象失效的客户端解决方法

    ASP(Active Server Pages)技术的Session对象用于存储用户在对话期间的私有信息.当前用户的Session对象中定义的变量和对象能在页面之间共享,但是不能为应用中其他用户所访问 ...

  4. Runtime.getRuntime().exec()----记录日志案例

    Runtime.getRuntime().exec()方法主要用于运行外部的程序或命令. Runtime.getRuntime().exec共同拥有六个重载方法: 1.public Process e ...

  5. PowerDesigner将PDM导出生成WORD文档(转)

    今天的温习老知识,是如何将一个PD设计的PDM来导出WORD文档,这是一个非常实用的功能,可以在软件过程的数据库设计文档编写中节省N多时间, 那不废话了,我们就开始今天的讲解吧! 第一步,点击Repo ...

  6. eclipse tomcat maven

    jdk jre eclipse 略过 下载maven和tomcat 上apache官网下载maven:http://maven.apache.org/download.cgi. 上apache官网下载 ...

  7. Python中urllib2总结

    使用Python访问网页主要有三种方式: urllib, urllib2, httpliburllib比较简单,功能相对也比较弱,httplib简单强大,但好像不支持session1. 最简单的页面访 ...

  8. 点击textbox弹出对话框,返回弹出对话框的值

    主要是在父页面使用 function PopupWindow() {            window.open(url, "", "status=no,resizab ...

  9. ios何时使用self.

     本文转载至  http://blog.csdn.net/lvxiangan/article/details/27204265   何时使用self.在网上搜索或者论坛里的回复大多都是简简单单的说这与 ...

  10. python 基础 5.1 python 构造器

    一. 类的构造器 __init__ 构造函数,在生成对象时调用.由于类可以起到模板的作用,因此,可以在创建实例的时候,把一些我们认为必须绑定的属性强制填写进去.通过定义一个特殊的__init__方法, ...