2048聚合版开源代码,cocos2d-js编写,基于CocosEditor开发工具,可运行Android,ios,html5等
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等的更多相关文章
- 【Node.js】2.开发Node.js选择哪个IDE 开发工具呢
安装完Node.js之后,就要为它选择一个有利的IDE用于开发. 相比较了多个IDE之后,定位在webstrom和sublime上. 有一个简单的比较: webstorm功能很丰富,前端开发工具的集大 ...
- 我使用过的Linux命令之swig - 把C/C++的代码嵌入Java等语言的开发工具
用途说明 SWIG是Simplified Wrapper and Interface Generator的缩写,其官方站点是http://www.swig.org/.SWIG是个帮助使用C或者C++编 ...
- 前端 go.js 流程图基于vue开发项目案例
一.流程图效果 最近一段时间在研究go.js,它是一款前端开发画流程图的一个插件,也是一个难点,要说为什么是难点,首先,它是依赖画布canvas知识开发.其次,要依赖于内部API开发需求,开发项目需求 ...
- CWMP开源代码研究6——libcwmp动态库开发
原创作品,转载请注明出处,严禁非法转载.如有错误,请留言! email:40879506@qq.com 为了使程序具有通用性,便于扩展和维护.采用了"模块"插入的思想.将设备业务相 ...
- Android 开源控件与常用开发框架开发工具类
Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...
- 用HTML5/CSS3/JS开发Android/IOS应用框架大全
现在人人都想成为安卓/IOS应用开发工程师.其实,安卓/IOS应用可以用很多种语言来实现.由于我们前端开发工程师,对HTML5/CSS/JavaScript的网络编程已经相当熟悉了.所以,今天大家将会 ...
- 求推荐go语言开发工具及go语言应该以哪种目录结构组织代码?
go语言的开发工具推荐? go语言开发普通程序及开发web程序的时候,应该以哪种目录结构组织代码? 求推荐go语言开发工具及go语言应该以哪种目录结构组织代码? >> golang这个答案 ...
- 火星坐标、百度坐标、WGS84坐标转换代码(JS、python版)
火星坐标.百度坐标.WGS84坐标转换代码(JS.python版) 一.JS版本源码 github:https://github.com/wandergis/coordTransform /** * ...
- 前端、HTML+CSS+JS编写规范(终极版)
HTMLCSS文档规范 HTML和CSS文档必须采用UTF-8编码格式: HTML文档必须使用HTML5的标准文档格式: HTMLCSS编写规范 HTML和CSS的标签.属性.类名.ID都必须使用小写 ...
随机推荐
- [转载]使用RoboCopy 命令
经常进行文件管理操作的朋友们,不满意于Windows系统内置的复制功能,因为它太龟速了.于是大家就使用FastCopy.TeraCopy之类的软件来加速复制,但是你是否知道Windows 7已经内置快 ...
- csdn开源夏令营-ospaf中期报告
1.背景 随着将中期的代码托管到CSDN的平台上,ospaf(开源项目成熟度分析工具)已经有了小小的雏形.当然还远远不够. 首先还是要感谢这次活动组织方CSDN,感觉挺有G ...
- 地图之CLLocationManager的使用 定位功能使用
1.iOS8曾经使用CLLocationManager 1.导入头文件 <CoreLocation/CoreLocation.h> 2.创建位置管理者 CLLocationManager ...
- Oracle中没有 if exists(...)的解决方法
http://blog.csdn.net/hollboy/article/details/7550171对于Oracle中没有 if exists(...) 的语法,目前有许多种解决方法,这里先分析常 ...
- JS图片预加载插件
在开发H5项目中有时候会遇到要加载大量图片的情况,利用预加载技术可以提高用户浏览时的体验. 1)概念:懒加载也叫延迟加载:JS图片延迟加载,延迟加载图片或符合某些条件时才加载某些图片.预加载:提前加载 ...
- Robot Framework使用Phantomjs进行无界面UI自动化测试
Robot Framework 是一款关键字驱动的验收自动化测试框架,现在在国内使用的越来越广泛了.一种通用的Web UI自动化测试解决方案是Robot Framework+Selenium2Libr ...
- python之脚本参数optparse
import optparse usage = "myprog[ -f <filename>][-s <xyz>] arg1[,arg2..]" opter ...
- iOS7 文本转语音 AVSpeechSynthesizer -转载-
http://www.cnblogs.com/qingjoin/p/3160945.html iOS7 的这个功能确实不错.我刚试了下,用官方提供的API ,简单的几句代码就能实现文本转语音! Xco ...
- win本地配置docker环境
先上官网链接:https://docs.docker.com/get-started/part2/#introduction 优质入门教程:http://www.docker.org.cn/book/ ...
- Python 之 安装模块的多种方法
1.自己写的模块,能够直接加入到路径下. 这样就能够直接调用. import sys sys.path.append("/home/username/") 2.单文件模块 直接把文 ...