6.移动所有身体
if (SNAKE_BODY.length !=0){
var Snode = null;
for (var i = SNAKE_BODY.length - 1; i>=0; i--){ //为什么从后开始遍历
Snode = SNAKE_BODY[i];
if (i==0){
switch (dir){
case SNAKE_DIR.UP:
Snode.now_row = this._head.now_row - 1; //向上走得话蛇身在蛇头下一行
Snode.now_col = this._head.now_col;
break;
case SNAKE_DIR.DOWN:
Snode.now_row = this._head.now_row + 1;
Snode.now_col = this._head.now_col;
break;
case SNAKE_DIR.LEFT:
Snode.now_row = this._head.now_row;
Snode.now_col = this._head.now_col + 1; //向左走得话蛇身在蛇头右边一列,所以+1
break;
case SNAKE_DIR.RIGHT:
Snode.now_row = this._head.now_row;
Snode.now_col = this._head.now_col - 1;
break;
default: break;
}
}else{
Snode.now_col = SNAKE_BODY[i-1].now_col;
Snode.now_row = SNAKE_BODY[i-1].now_row;
}
Snode.setPosition(cc.p(Snode.now_col*63,Snode.now_row*63));
}
}
-----------------------------------------------------------------------------
7.死亡判定之蛇头碰蛇身以及触摸回调
//蛇头碰蛇身
if (SNAKE_BODY.length !=0){
for (var i = SNAKE_BODY.length - 1;i>=0; i--){
if (this._head.now_col == SNAKE_BODY[i].now_col && this._head.now_row == SNAKE_BODY[i].now_row){
this.onGameOver();
}
}
}
//GameOver
onGameOver:function(){
director.pause(); //暂停游戏
var gameOver = new cc.LabelTTF("Game Over","Arial",50);
gameOver.setPosition(winSize.width/2,winSize.height/2);
this.addChild(gameOver);
}
//触摸回调
onTouchBegan:function(touch,event){
var target = event.getCurrentTarget();
var mx = Math.abs((touch.getLocation().x-5)-target._head.now_col*63);
var my = Math.abs((touch.getLocation().y-250)-target._head.now_row*63);
if (mx>my) { //左右移动
if((touch.getLocation().x-5)>target._head.now_col*63){ //触摸横坐标要减去背景层横坐标5
dir = SNAKE_DIR.RIGHT;
}else{
dir = SNAKE_DIR.LEFT;
}
}else{ //上下移动
if((touch.getLocation().y-250)>target._head.now_row*63){ //触摸高度要减去背景层高度250
dir = SNAKE_DIR.UP;
}else{
dir = SNAKE_DIR.DOWN;
}
}
if (SNAKE_BODY.length !=0){
var Snode = null;
for (var i = SNAKE_BODY.length - 1; i>=0; i--){ //为什么从后开始遍历
Snode = SNAKE_BODY[i];
if (i==0){
switch (dir){
case SNAKE_DIR.UP:
Snode.now_row = this._head.now_row - 1; //向上走得话蛇身在蛇头下一行
Snode.now_col = this._head.now_col;
break;
case SNAKE_DIR.DOWN:
Snode.now_row = this._head.now_row + 1;
Snode.now_col = this._head.now_col;
break;
case SNAKE_DIR.LEFT:
Snode.now_row = this._head.now_row;
Snode.now_col = this._head.now_col + 1; //向左走得话蛇身在蛇头右边一列,所以+1
break;
case SNAKE_DIR.RIGHT:
Snode.now_row = this._head.now_row;
Snode.now_col = this._head.now_col - 1;
break;
default: break;
}
}else{
Snode.now_col = SNAKE_BODY[i-1].now_col;
Snode.now_row = SNAKE_BODY[i-1].now_row;
}
Snode.setPosition(cc.p(Snode.now_col*63,Snode.now_row*63));
}
}
-----------------------------------------------------------------------------
7.死亡判定之蛇头碰蛇身以及触摸回调
//蛇头碰蛇身
if (SNAKE_BODY.length !=0){
for (var i = SNAKE_BODY.length - 1;i>=0; i--){
if (this._head.now_col == SNAKE_BODY[i].now_col && this._head.now_row == SNAKE_BODY[i].now_row){
this.onGameOver();
}
}
}
//GameOver
onGameOver:function(){
director.pause(); //暂停游戏
var gameOver = new cc.LabelTTF("Game Over","Arial",50);
gameOver.setPosition(winSize.width/2,winSize.height/2);
this.addChild(gameOver);
}
//触摸回调
onTouchBegan:function(touch,event){
var target = event.getCurrentTarget();
var mx = Math.abs((touch.getLocation().x-5)-target._head.now_col*63);
var my = Math.abs((touch.getLocation().y-250)-target._head.now_row*63);
if (mx>my) { //左右移动
if((touch.getLocation().x-5)>target._head.now_col*63){ //触摸横坐标要减去背景层横坐标5
dir = SNAKE_DIR.RIGHT;
}else{
dir = SNAKE_DIR.LEFT;
}
}else{ //上下移动
if((touch.getLocation().y-250)>target._head.now_row*63){ //触摸高度要减去背景层高度250
dir = SNAKE_DIR.UP;
}else{
dir = SNAKE_DIR.DOWN;
}
}
}
---------------------------菜鸟写程序,还请大家多多指教---------------------------