Cocos2d-js05-添加身体和移动身体

前端之家收集整理的这篇文章主要介绍了Cocos2d-js05-添加身体和移动身体前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Cocos2d-js05-添加身体和移动身体

1、检测蛇是否吃到食物,代码

 //检测蛇是否吃到食物 if(this._head.now_col==this._food.now_col && this._head.now_row==this._food.now_row){
 //播放音效  cc.audioEngine.playEffect(res.bg_effect);
 cc.log("蛇吃到了食物!");
 //添加分数  this.m_score += 100;
 score.setString("分数:"+this.m_score);
 //重置食物的位置  this._food.now_row = Math.round(Math.random()*9);
 this._food.now_col = Math.round(Math.random()*9);
 this._food.setPosition(cc.p(this._food.now_col*63,this._food.now_row*63));

 //添加蛇的身体  this._snakeBody = SnakeGame.create(2);
 this._snakeBody.setScale(0.9);
 if(SNAKE_BODY.length == 0 || SNAKE_BODY.length == null){
 switch (dir){
 case SNAKE_DIR.UP:
 this._snakeBody.now_row = this._head.now_row - 1;
 this._snakeBody.now_col = this._head.now_col;
 break;
 case SNAKE_DIR.DOWN:
 this._snakeBody.now_row = this._head.now_row + 1;
 this._snakeBody.now_col = this._head.now_col;
 break;
 case SNAKE_DIR.LEFT:
 this._snakeBody.now_row = this._head.now_row;
 this._snakeBody.now_col = this._head.now_col + 1;
 break;
 case SNAKE_DIR.RIGHT:
 this._snakeBody.now_row = this._head.now_row;
 this._snakeBody.now_col = this._head.now_col - 1;
 break;
 default :break;
 }
 cc.log("里面没有身体,添加一个!");

 }else{
 switch (dir){
 case SNAKE_DIR.UP:
 this._snakeBody.now_row = this._snakeBody.now_row - 1;
 this._snakeBody.now_col = this._snakeBody.now_col;
 break;
 case SNAKE_DIR.DOWN:
 this._snakeBody.now_row = this._snakeBody.now_row + 1;
 this._snakeBody.now_col = this._snakeBody.now_col;
 break;
 case SNAKE_DIR.LEFT:
 this._snakeBody.now_row = this._snakeBody.now_row;
 this._snakeBody.now_col = this._snakeBody.now_col + 1;
 break;
 case SNAKE_DIR.RIGHT:
 this._snakeBody.now_row = this._snakeBody.now_row;
 this._snakeBody.now_col = this._snakeBody.now_col - 1;
 break;
 default :break;
 }
 cc.log("里面有身体,添加一个!");
 }
 //添加到数组中去  SNAKE_BODY.push(this._snakeBody);
 this.getChildByTag(111).addChild(this._snakeBody,2);
 this._snakeBody.setPosition(cc.p(this._snakeBody.now_col*63,this._snakeBody.now_row*63));
}

2、移动所有的身体,代码

//移动所有的身体 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;
 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));
 }
}
 
 
视频地址:http://www.9miaoketang.com/course/37
课程讨论帖地址:http://www.9miao.com/thread-64587-1-1.html
源码地址:https://store.cocos.com/stuff/show/128289.html
QQ交流群:83459374
后期也会把该源码传在群里面去,欢迎大家加入讨论!
原文链接:https://www.f2er.com/cocos2dx/342488.html

猜你在找的Cocos2d-x相关文章