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
后期也会把该源码传在群里面去,欢迎大家加入讨论!

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...